今天在解答知道上面的问题时,看到有人问TP中query 和 execute 的区别!~
于是自己看了下源码,主要区别就在查询的最后!
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 方法都是这样吧!~~毕竟用的是PDO 查询!~~