与
ESC
输入关键词搜索文章

ThinkPHP中execute和query方法的区别

全文约 209 字预计阅读 1 分钟
ThinkPHP中execute()和query()方法都可以在参数里直接输入SQL语句。但是不同的是execute()通常用来执行insert或update等SQL语句,而query常用来执行select等语句。 execute()方法将返回影响的记录数,如果执行SQL的select语句的话,返回的结果将是表的总记录数: $model = M( "MyTable" ); $result = $model ->execute( 'update MyTable set name='a' where id=1); //将返回总行数 query()方法将返回数据集: $model = M( "MyTable" ); $result = $model ->query( 'select * from  MyTable' ); //将返回array() 查看源码还发现发现开头$this->initConnect();区别,query执行false ,execute执行ture /** * 初始化数据库连接 * @access protected * @param boolean $master 主服务器 * @return void */ protected function initConnect($master=true) { if(!empty($this->config['deploy'])) // 采用分布式数据库 $this->_linkID = $this->multiConnect($master); else // 默认单数据库 if ( !$this->_linkID ) $this->_linkID = $this->connect(); }
ThinkPHP中execute和query方法的区别
分享

发表评论

评论 0 条

暂无评论,来说点什么吧。