duanli12176 2014-11-18 05:32
浏览 66

如何验证MySQLi-> prepare()的返回值

I've searched high and low for an answer to this question, to no avail. I reach out to you my fellow coders. :-)

I have the following, seemingly simple PHP code:

$query = "SELECT marketid, type, quantity, price, datetime, bundleid, state, stoplossprice FROM orders WHERE type = ? AND state = ?";
$stmt = $login->mySQLi->prepare($query);
if(!$stmt) {
    throw new Exception(mysqli_error($login->mySQLi));
}
$stmt->bind_param("ss", $strOrderType, $strOrderState);
if($stmt)
{
    if(!$stmt->execute())
    {
        // Return an error
    }
}
$stmt->store_result();
$stmt->bind_result($marketid, $ordertype, $quantity, $buyprice, $buydatetime, $bundleid, $orderstate, $stoplossprice);
while ($stmt->fetch()) {
    // Do something with the returned data
}
$stmt->close();

I also have almost the exact same code several other places, and in the other places it works. But, for some reason it's terminating when executing the execute() statement.

Also, after executing the prepare() statement, $stmt->affected_rows equals -1.

I read in the MySQLi docs, a value of -1 means "-1 indicates that the query returned an error". But, for I can't figure out how to output the error message from $stmt. $stmt->errno and $stmt->error have no values.

I've also checked the log file for MySQL and it shows the following:

141117 21:21:39 222 Prepare SELECT marketid, type, quantity, price, datetime, bundleid, state, stoplossprice FROM orders WHERE type = ? AND state = ? 222 Statistics 223 Statistics
141117 21:21:54 222 Statistics
223 Statistics
141117 21:21:55 222 Statistics
223 Statistics
141117 21:21:56 222 Statistics
223 Statistics
141117 21:21:57 222 Execute SELECT marketid, type, quantity, price, datetime, bundleid, state, stoplossprice FROM orders WHERE type = 'Buy' AND state = 'Pending' 223 Quit
222 Close stmt
222 Quit

Help me Obi-wan Kenobi, you're my only hope (and yah, it's late, I'm feeling a bit slappy)!

  • 写回答

1条回答 默认 最新

  • drcb19700 2014-11-19 04:02
    关注

    I was, at long last, able to find the answer.

    Prior to executing the code I posted, I was executing other MySQL statements. In those statements I executed mysqli_report(MYSQLI_REPORT_ALL);.

    As I learned in the PHP docs, one should be careful when calling mysqli_report(MYSQLI_REPORT_ALL);. Here's an excerpt from the documentation of mysqli_report(), explaining the issue:

    "Be very careful using this function - it's a per-process setting. If your server is set up to reuse a single PHP process for multiple requests, that means the last setting of this function in any script will affect all other scripts using mysqli. To be safe always call at the end of a script. The CGI version of PHP is probably safe from this."

    I added mysqli_report(MYSQLI_REPORT_OFF); to the end of the 1st MySQL statements and now it works like a charm. Thanks PHP docs!! And thanks to sectus and Antoan Milkov for your efforts as well!!

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改