Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging MySQL response from PHP page

Tags:

php

mysql

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
like image 478
shantanuo Avatar asked Mar 08 '26 02:03

shantanuo


2 Answers

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());
like image 114
Daniel Vassallo Avatar answered Mar 10 '26 15:03

Daniel Vassallo


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().

like image 34
Ignacio Vazquez-Abrams Avatar answered Mar 10 '26 14:03

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!