我怎么知道新行插入我的mysql数据库使用php


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

我已经使用php创建了一个聊天框。它工作得很顺利。但我不知道当新的消息插入我的mysql数据库。我怎么知道?我已经试过了。但是它总是回我新信息。

$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";}

插入到数据库后,mysqli应该返回受影响的行数,您可以使用它来知道是否有新的数据插入到数据库中。

该数字存储在$mysqli->affected_rows

例子:

<?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
?>