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);
but it says that Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
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);
but it says that Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
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).
mysql_affected_rows with the connection identifier, not the result record, as written on php.net/manual/en/function.mysql-affected-rows.phpYou 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;