PHP / mysql获取UPDATE语句的受影响行数

时间:2022-09-28 22:56:14

With php/mysql how can i get the number of rows that a query affected?

使用php / mysql如何获得查询影响的行数?

what i tried so far:

到目前为止我尝试了什么:

$result = mysql_query($q);
mysql_num_rows($result);

but it says that Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

但它说警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源

5 个解决方案

#1


15  

if you're using PDO (wich i would recommend), for a direct query exec() returns the number of affected rows. for Prepared Statements theres a method called rowCount().

如果您正在使用PDO(我建议使用),对于直接查询,exec()会返回受影响的行数。对于Prepared Statements,有一个名为rowCount()的方法。

if you're using the mysql-functions, there's mysql_affected_rows().

如果你使用的是mysql函数,那就是mysql_affected_rows()。

EDIT:
seems like you're using the mysql-functions. mysql_num_rows, wich is what you're using, returns the length of your result set (for SELECT-Statements for example). what you need to use is mysql_affected_rows (as already said).

编辑:好像你正在使用mysql函数。 mysql_num_rows,您正在使用的,返回结果集的长度(例如,对于SELECT-Statements)。您需要使用的是mysql_affected_rows(如前所述)。

#2


6  

You also may want to use a ROW_COUNT() function, e.g. -

您也可能想要使用ROW_COUNT()函数,例如 -

UPDATE table1 SET column1 = 100 WHERE column2 = 10;
SELECT ROW_COUNT();

From the reference - ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT...

从引用 - ROW_COUNT()返回最后一个语句更改,删除或插入的行数(如果它是UPDATE,DELETE或INSERT)...

#3


5  

You needs mysql_affected_rows

你需要mysql_affected_rows

#4


2  

clamp,

钳,

you need to supply a resource to mysql_affected_rows, not a result record. See the links that the others have posted for additional information.

您需要为mysql_affected_rows提供资源,而不是结果记录。有关其他信息,请参阅其他人发布的链接。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query($q);
echo mysql_affected_rows($link);

#5


0  

Whe can also do it using PDO :

也可以使用PDO来做到这一点:

$db = new PDO('', '', '')// your connection
$sql = "UPDATE tb_table SET rowname = value WHERE rowid = 1";
$query = $db->query($sql);
echo $query;

#1


15  

if you're using PDO (wich i would recommend), for a direct query exec() returns the number of affected rows. for Prepared Statements theres a method called rowCount().

如果您正在使用PDO(我建议使用),对于直接查询,exec()会返回受影响的行数。对于Prepared Statements,有一个名为rowCount()的方法。

if you're using the mysql-functions, there's mysql_affected_rows().

如果你使用的是mysql函数,那就是mysql_affected_rows()。

EDIT:
seems like you're using the mysql-functions. mysql_num_rows, wich is what you're using, returns the length of your result set (for SELECT-Statements for example). what you need to use is mysql_affected_rows (as already said).

编辑:好像你正在使用mysql函数。 mysql_num_rows,您正在使用的,返回结果集的长度(例如,对于SELECT-Statements)。您需要使用的是mysql_affected_rows(如前所述)。

#2


6  

You also may want to use a ROW_COUNT() function, e.g. -

您也可能想要使用ROW_COUNT()函数,例如 -

UPDATE table1 SET column1 = 100 WHERE column2 = 10;
SELECT ROW_COUNT();

From the reference - ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT...

从引用 - ROW_COUNT()返回最后一个语句更改,删除或插入的行数(如果它是UPDATE,DELETE或INSERT)...

#3


5  

You needs mysql_affected_rows

你需要mysql_affected_rows

#4


2  

clamp,

钳,

you need to supply a resource to mysql_affected_rows, not a result record. See the links that the others have posted for additional information.

您需要为mysql_affected_rows提供资源,而不是结果记录。有关其他信息,请参阅其他人发布的链接。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query($q);
echo mysql_affected_rows($link);

#5


0  

Whe can also do it using PDO :

也可以使用PDO来做到这一点:

$db = new PDO('', '', '')// your connection
$sql = "UPDATE tb_table SET rowname = value WHERE rowid = 1";
$query = $db->query($sql);
echo $query;