0

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";}
nirob
  • 11
  • 2
  • You calculate `$msgcounter = $initialCounter + $countMsg;` and then wonder why `$msgcounter > $initialCounter` one line after that? It will always be greater if `$countMsg > 0`, by exactly `$countMsg`. It is not really clear what you are trying to do. You should maybe compare the new count to a value you stored somewhere. – Solarflare Oct 26 '16 at 09:59
  • @Solarflare Here select query use for message read.but how can i understand new message available? any idea? – nirob Oct 26 '16 at 10:27

1 Answers1

0

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
?>
Lorence Hernandez
  • 1,189
  • 12
  • 23
  • i know that.but i want to know when select query executed.because of select query use for message read.but how can i understand new message available? any idea? – nirob Oct 26 '16 at 10:24