PHP, MySQL, PDO - Get result from UPDATE query?

后端 未结 3 1436
说谎
说谎 2020-12-20 11:29

I am updating a row in a table, and trying to return the updated row, as per this SO answer.

My code is the following:

$sql = \"SET @update_id := \'\         


        
3条回答
  •  無奈伤痛
    2020-12-20 12:19

    You're right about getting the exception SQLSTATE[HY000] for $stmt->rowCount();

    The problem is, you cannot fetch an UPDATE query because these queries simply don't return values. To circumvent this, use rowCount().

    As written in the PHP documentation, PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement.

    Check out this example.

    prepare('UPDATE ... PICNIC');
    $update->execute();
    
    /* Return the number of rows affected */
       echo $updateCount = $update->rowCount();
    
    ?>
    

提交回复
热议问题