简体   繁体   中英

how can i know new row insert my mysql database using php?

I have create a chatbox using php.its smoothly works.but i don't know when new message inserted my mysql database.How can i know that? i've already tried to below this code.but its always return me new message.

$initialCounter = 0;
$count_query = "select * from `message` where `receiver_id` = '$sender_id' AND `sender_id` = '$rid' order by `id` desc";
$count_query_res = $conn->query($count_query);
$countMsg = $count_query_res->num_rows;
$initialCounter += $countMsg;
$msgcounter = $initialCounter + $countMsg;
if($initialCounter<$msgcounter){ echo "new message";}

After inserting to the database, mysqli should return the number of affected rows which you can use to know if theres a new data inserted in your database.

that number is stored in $mysqli->affected_rows

Example:

<?php
$c = new mysqli("server","username","password","database");

$c->query("insert into table(column1,column2,column3) values('data','data','data')");

echo $c->affected_rows; // returns the affected rows
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM