开发者

Can SQL tell me how many row(s) were updated?

I would use only a single query:

$sq开发者_运维问答l = "UPDATE gallery SET order = (order+1) WHERE id_categ = ".$id;
$res = mysql_query($sql);

...

Is it possible?


mysql_affected_rows()

mysql_affected_rows — Get number of affected rows in 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 the mysql_affected_rows() return you the affected rows whether updated/deleted of your last run query. For example:

$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 that is gallery2 table's query.

More Info:

  • http://php.net/manual/en/function.mysql-affected-rows.php


mysql_affected_rows() should to the job.

Copied from the manual:

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


You can have a look at something like

  • mysql_affected_rows
  • PHP mysql_affected_rows() Function


Yes, just use

mysql_affected_rows()

after your query.

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

Edit: Damn, you guys are fast ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜