mysql pdo支持,支持多个查询(PDO_MySQL,PDO_MYSQLND)

经过半天的摆弄,发现PDO有个错误.

--//This would run as expected:$pdo->exec("valid-stmt1; valid-stmt2;");

--//This would error out, as expected:$pdo->exec("non-sense; valid-stmt1;");

--//Here is the bug:$pdo->exec("valid-stmt1; non-sense; valid-stmt3;");

它将执行"valid-stmt1;"停下来"non-sense;"永远不要犯错误。不会运行"valid-stmt3;"还真撒谎说一切都很顺利。

我希望它在"non-sense;"但事实并非如此。

这里是我找到这个信息的地方:无效的PDO查询不返回错误

所以,我试着用mysqli来做这件事,但是没有找到任何关于它是如何工作的确切答案,所以我想我把它留给那些想要使用它的人。try{

// db connection

$mysqli = new mysqli("host", "user" , "password", "database");

if($mysqli->connect_errno){

throw new Exception("Connection Failed: [".$mysqli->connect_errno. "] : ".$mysqli->connect_error );

exit();

}

// read file.

// This file has multiple sql statements.

$file_sql = file_get_contents("filename.sql");

if($file_sql == "null" || empty($file_sql) || strlen($file_sql) <= 0){

throw new Exception("File is empty. I wont run it..");

}

//run the sql file contents through the mysqli's multi_query function.

// here is where it gets complicated...

// if the first query has errors, here is where you get it.

$sqlFileResult = $mysqli->multi_query($file_sql);

// this returns false only if there are errros on first sql statement, it doesn't care about the rest of the sql statements.

$sqlCount = 1;

if( $sqlFileResult == false ){

throw new Exception("File: '".$fullpath."' , Query#[".$sqlCount."], [".$mysqli->errno."]: '".$mysqli->error."' }");

}

// so handle the errors on the subsequent statements like this.

// while I have more results. This will start from the second sql statement. The first statement errors are thrown above on the

$mysqli->multi_query("SQL"); line

while($mysqli->more_results()){

$sqlCount++;

// load the next result set into mysqli's active buffer. if this fails the $mysqli->error, $mysqli->errno will have appropriate

error info.

if($mysqli->next_result() == false){

throw new Exception("File: '".$fullpath."' , Query#[".$sqlCount."], Error No: [".$mysqli->errno."]: '".$mysqli->error."' }");

}

}}catch(Exception $e){

echo $e->getMessage(). " 

".$e->getTraceAsString()."
";}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值