dth2331 2014-08-06 18:44
浏览 177
已采纳

PHP PDO MySQL在没有行受影响时检查更新查询是否成功的正确方法

What is a sure way to tell if an update query succeeded when using php pdo and mysql?

In my app, I update totals of items that are submitted but a user, and the table looks like this:

items

userId | itemsAdded | itemsChecked | itemsUnChecked | itemsTotal
     1 |          5 |            2 |              3 |          5

So when I do update items set itemTotals = itemsChecked+itemUnChecked the itemsTotal column remains the same unless the itemsAdded changes and the itemsUnChecked increments (2 + 3 equals 5, 1 + 4 is also 5).

I used to use rowCount() to check if a query succeeded, but in this case, since the itemsTotal column stays the same, there's no way of telling if the sql succeeded or not.

$query = $conn->prepare($sql);

$query->execute(array(

    ":itemCount" => $itemCount
    ":itemId" => $itemId
));

$queryCount = $query->rowCount();

if($queryCount == 1) {
    echo 'Updated succeeded';
} else {
    echo 'Updated failed!';
}

I could also use:

$query = $conn->prepare($sql);

$result = $query->execute(array(

    ":itemCount" => $itemCount
    ":itemId" => $itemId
));

if($result) {
    echo 'Updated succeeded';
} else {
    echo 'Updated failed!';
}

But does that return true or false based on if the query succeed or based on the number of rows it updated?

I only need to check if the query succeeded or not. No need to tell the number of rows that were updated.

  • 写回答

2条回答 默认 最新

  • douzuizhuo0587 2014-08-06 19:28
    关注

    The execute() method will either throw an exception or return FALSE (depending on the error mode you have set for the database connection) when the execution of a SQL statement fails.

    If we set the error mode to throw an exception, prior to executing a statement, (usually immediately after establishing a database connection), like this

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    Then we can use a try/catch block to handle an exception thrown by a SQL statement. Something like this:

    try {
      $query->execute(...);
      echo 'Update succeeded';
    
    } catch(PDOException $e) {
      echo 'Update failed!';
      echo 'Error: ' . $e->getMessage();
    }
    

    Information about error mode settings and handling is available in the documentation here: http://php.net/manual/en/pdo.error-handling.php


    Or, if PDO isn't set to throw an exception, we can use a simple if test. (The execute() method will return FALSE if the the statement fails.)

    if ($query->execute(...)) {
       echo 'Update succeeded';
    
    } else {
       echo 'Update failed!';
    
    }
    

    For more precise control with the different types of failure, we can use the errorCode() method to retrieve the SQLSTATE associated with the last operation on the statement handle, and we can perform conditional tests on the returned value. http://php.net/manual/en/pdostatement.errorcode.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?