Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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 successful or not. I thought I could use mysql_affected_rows...

// Example:
// After running query "UPDATE mytable SET name='Test' WHERE ID=1"
$result = mysql_affected_rows();
if ($result >= 1) { /* Success */ }

If, for example, there was no row with ID=1, then $result would be 0.

However, it turns out that PHP's mysql_affected_rows is the actual affected rows, and may be still be 0 if the row exists but name was already "Test". (The PHP docs even say this is the case).

If I run this in the command line, I get the following meta information about the query:

Query OK, 0 rows affected (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Is there any way for me to get that "Rows matched" value in PHP instead of the affected rows?

[Edit]: I should note that I know I can run a separate query, but I'd like to not do that, for the sake of performance.

like image 348
Nicole Avatar asked May 27 '10 23:05

Nicole


People also ask

How check row is affected in MySQL?

mysql_affected_rows() may be called immediately after executing a statement with mysql_real_query() or mysql_query() . It returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE , DELETE , or INSERT . For SELECT statements, mysql_affected_rows() works like mysql_num_rows() .

How can I count the number of rows affected in PHP?

The affected_rows / mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

Which of the following is used to display number of rows affected by a query?

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query.

How can check rows affected in codeigniter?

$this->db->affected_rows() Displays the number of affected rows, when doing "write" type queries (insert, update, etc.). Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows.


2 Answers

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).

like image 129
Vinicius Pinto Avatar answered Nov 09 '22 07:11

Vinicius Pinto


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!

like image 26
Alessandro Villa Avatar answered Nov 09 '22 09:11

Alessandro Villa