With php/mysql how can i get the number of rows that a query affected?
what i tried so far:
$result = mysql_query($q);
mysql_num_rows($result);
You also may want to use a ROW_COUNT() function, e.g. -
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...
Whe can also do it using PDO :
$db = new PDO('', '', '')// your connection
$sql = "UPDATE tb_table SET rowname = value WHERE rowid = 1";
$query = $db->query($sql);
echo $query;
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.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query($q);
echo mysql_affected_rows($link);
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().
if you're using the mysql-functions, there's 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).
You needs mysql_affected_rows