sql 原生php执行语句,Thinkphp执行原生的sql语句

query重点是查询功能

代码功能如下:

/**

* 执行查询 返回数据集

* @access public

* @param string $str sql指令

* @param boolean $fetchSql 不执行只是获取SQL

* @return mixed

*/

public function query($str,$fetchSql=false) {

$this->initConnect(false); //初始化数据库的连接,也就是数据库服务器的部署方式,是单一数据库还是主从//部署,关于该函数,见下面:

if ( !$this->_linkID ) return false;

$this->queryStr = $str; //原生sql语句

if(!empty($this->bind)){

$that = $this;

$this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind));

}

if($fetchSql){

return $this->queryStr;

}

//释放前次的查询结果

if ( !empty($this->PDOStatement) ) $this->free();

$this->queryTimes++;

N('db_query',1); // 兼容代码

// 调试开始

$this->debug(true);

$this->PDOStatement = $this->_linkID->prepare($str);

if(false === $this->PDOStatement){

$this->error();

return false;

}

foreach ($this->bind as $key => $val) {

if(is_array($val)){

$this->PDOStatement->bindValue($key, $val[0], $val[1]);

}else{

$this->PDOStatement->bindValue($key, $val);

}

}

$this->bind = array();

try{

$result = $this->PDOStatement->execute();

// 调试结束

$this->debug(false);

if ( false === $result ) {

$this->error();

return false;

} else {

return $this->getResult();

}

}catch (\PDOException $e) {

$this->error();

return false;

}

}

/**

* 初始化数据库连接

* @access protected

* @param boolean $master 主服务器

* @return void

*/

protected function initConnect($master=true) {

if(!empty($this->config['deploy'])) //config['deploy']服务器的部署方式 ,0表示单一,1表示主从配置

// 采用分布式数据库

$this->_linkID = $this->multiConnect($master);

else

// 默认单数据库

if ( !$this->_linkID ) $this->_linkID = $this->connect();

}

execute重点是增删改功能

代码:

/**

* 执行语句

* @access public

* @param string $str sql指令

* @param boolean $fetchSql 不执行只是获取SQL

* @return mixed

*/

public function execute($str,$fetchSql=false) {

$this->initConnect(true);

if ( !$this->_linkID ) return false;

$this->queryStr = $str;

if(!empty($this->bind)){

$that = $this;

$this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return '\''.$that->escapeString($val).'\''; },$this->bind));

}

if($fetchSql){

return $this->queryStr;

}

//释放前次的查询结果

if ( !empty($this->PDOStatement) ) $this->free();

$this->executeTimes++;

N('db_write',1); // 兼容代码

// 记录开始执行时间

$this->debug(true);

$this->PDOStatement = $this->_linkID->prepare($str);

if(false === $this->PDOStatement) {

$this->error();

return false;

}

foreach ($this->bind as $key => $val) {

if(is_array($val)){

$this->PDOStatement->bindValue($key, $val[0], $val[1]);

}else{

$this->PDOStatement->bindValue($key, $val);

}

}

$this->bind = array();

try{

$result = $this->PDOStatement->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;

}

}catch (\PDOException $e) {

$this->error();

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值