thinkphp3.2.3批量执行sql语句(带事务)
/**
* 事务封装方法
* @access public 将此方法放入框架model.class.php中
* @param array $sqls 要执行的sql数组或语句
* @param array $vals sql语句中要替换的值
* @return boolean
*/
public function transExecuteSql($sqls)
{
$this->startTrans();
if(is_array($sqls))
{
foreach($sqls as $k => $sql)
{
$result=$this->db->execute($sql);
if(!$result)
{
$this->rollBack();
return false;
}
}
}else{
$result=$this->db->execute($sqls);
if(!$result)
{
$this->rollBack();
return false;
}
}
$this->commit();
return true;
}
使用:
$sqlArray = array();
$valArray = array();
$sqlCoverPicture = 'insert into picture(id,objid,objtype,path) values(?,?,?,?)';
$valCoverPicture = array(1,2,,3,4);
array_push($sqlArray, $sqlCoverPicture);
array_push($valArray, $valCoverPicture);
D()->transExecuteSql($sqlArray, $valArray)