【问题标题】:how can i know new row insert my mysql database using php? [duplicate]我如何知道使用 php 插入我的 mysql 数据库的新行? [复制]
【发布时间】:2016-10-26 09:49:58
【问题描述】:

我已经使用 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";}

【问题讨论】:

  • 您计算了$msgcounter = $initialCounter + $countMsg;,然后想知道为什么$msgcounter &gt; $initialCounter 紧随其后一行?如果$countMsg &gt; 0$countMsg 相差无几,它总是会更大。目前还不清楚您要做什么。您可能应该将新计数与您存储在某处的值进行比较。
  • @Solarflare 在这里选择查询用于消息读取。但是我如何理解可用的新消息?有什么想法吗?

标签: php mysql


【解决方案1】:

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

该号码存储在$mysqli-&gt;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
?>

【讨论】:

  • 我知道。但我想知道选择查询何时执行。因为选择查询用于消息读取。但我如何理解可用的新消息?有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 2013-10-05
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多