tags:

views:

31

answers:

3

When I insert a record, I get the message "1 row affected" and while updating "Rows matched:1 Changed:1" How do I get these messages from PHP code?

mysql> insert into mytest values ('103');
Query OK, 1 row affected (0.26 sec)

mysql> update mytest set id = 12 where id = 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
+2  A: 

You can use the mysql_affected_rows() method:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('mydb');

mysql_query('insert into mytest values ('103');');
printf("Records inserted: %d\n", mysql_affected_rows());

mysql_query('update mytest set id = 12 where id = 10;');
printf("Records updated: %d\n", mysql_affected_rows());
Daniel Vassallo
I had to add the value in double quotes like... "103"
shantanuo
A: 

Check out mysql_affected_rows and mysqli_affected_rows

Dominic Barnes
+1  A: 

Those strings only exist in the context of the mysql CLI tool; they aren't actually sent by the server. See mysql_affected_rows() and mysql_num_rows().

Ignacio Vazquez-Abrams