PHP, MySQL - can you distinguish between rows matched and rows affected?

后端 未结 2 1902
情书的邮戳
情书的邮戳 2020-12-06 05:44

I am trying to write a PHP-MySQL database processor that is somewhat intelligent. When this processor decides it needs to make an update, I want to report if it was really

相关标签:
2条回答
  • 2020-12-06 06:23

    From the MySQL documentation for mysql_affected_rows:

    For UPDATE statements, if you specify the CLIENT_FOUND_ROWS flag when connecting to mysqld, mysql_affected_rows() returns the number of rows matched by the WHERE clause. Otherwise, the default behavior is to return the number of rows actually changed.

    With mysqli, you can specify the CLIENT_FOUND_ROWS using mysqli::real_connect.

    $db = mysqli_init();
    $db->real_connect('host', 'username', 'password', 'dbname', '3306', null, MYSQLI_CLIENT_FOUND_ROWS);
    

    In PDO, the constant is named PDO::MYSQL_ATTR_FOUND_ROWS

    $db = new PDO('mysql:dbname=mydatabase;host=myhost', 'username', 'password', array(
        PDO::MYSQL_ATTR_FOUND_ROWS => true
    ));
    

    With the old and deprecated MySQL extension, you can specify the CLIENT_FOUND_ROWS passing the value 2 as the 5th parameter for mysql_connect (source).

    0 讨论(0)
  • 2020-12-06 06:24

    You can also use the function

    $variable = mysql_info();
    

    That function retrieves a string like this:

    Rows matched: 1 Changed: 0 Warnings: 0

    You can work with strings functions on your variable to extract the substring with the number of rows that matched and you should have it!

    0 讨论(0)
提交回复
热议问题