下面整理mysql中query() 和execute() 的区别:
于是自己看了thinkphp源码,其他语言就算实现不同,原理应该相同,在thinkphp中,这两个函数的主要区别就在查询的最后:
query中:
// 调试结束
$this->debug(false);
if ( false === $result ) {
$this->error();
return false;
} else {
return $this->getResult();
}
execute中:
// 调试结束
$this->debug(false);
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = $this->PDOStatement->rowCount();
if(preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $str)) {
$this->lastInsID = $this->_linkID->lastInsertId();
}
return $this->numRows;
}
可以看到,两者的最后返回结果是不一样的,query 返回的是一个结果集,而execute 返回的是影响行数 或者 是插入数据的id !~由此可以看出,query 拿来执行 select 更好一些,execute 哪里执行 update | insert | delete 更好些!~~
应该大部分封装好的query 和 execute 方法都是这样吧!
转自:http://blog.youkuaiyun.com/qin_jian_bo/article/details/52230735