Can SQL tell me how many rows have been updated?

I would use only one query:

$sql = "UPDATE gallery SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);

...

Is it possible?

+3
source share
4 answers

mysql_affected_rows()

mysql_affected_rows - Get the number of rows affected in a previous MySQL operation

Example:

$sql = "UPDATE gallery SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);
printf("Affected rows: %d\n", mysql_affected_rows());

Note that mysql_affected_rows()returns you the affected rows updated / deleted from your last request . For instance:

$sql = "UPDATE gallery SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);

$sql = "UPDATE gallery2 SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);

Now if you do:

printf("Affected rows: %d\n", mysql_affected_rows());

It will return the affected rows for the last query, which is gallery2a table query.

Additional Information:

+4
source

mysql_affected_rows() should be on a mission.

Copied from manual:

<?php
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n", mysql_affected_rows());
// Prints: "Updated Records: 10"
+3

Yes just use

mysql_affected_rows ()

after your request.

$sql = "UPDATE gallery SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);
$rowsAffected = mysql_affected_rows();

Edit: Damn guys you fast;)

+1
source

Source: https://habr.com/ru/post/1759357/


All Articles