Reputation: 91
I need advice about the title above. I have the following piece of code :
PHP :
$sql = "SELECT * FROM i_case";
$qry = pg_query($connection, $sql);
$res = pg_num_rows($qry);
$sql_1 = "SELECT * FROM i_case";
$qry_1 = pg_query($connection, $sql_1);
$res_1 = pg_num_rows($qry_1);
do {
echo "new record on i_case";
} while($res != $res_1 and !(usleep(10000)));
JQUERY :
setInterval(function(){
$.ajax({
type : "POST",
url : "file.php",
success : function(response){
alert(response);
}
});
},1000);
It's not work that I expecting.
I don't know where is the matter from the code?
Upvotes: 3
Views: 23276
Reputation: 53
A little tweak to xdazz answer. To avoid alert for first time when you load the page.
JS Code:
var old_count = 0;
var i = 0;
setInterval(function(){
$.ajax({
type : "POST",
url : "notify.php",
success : function(data){
if (data > old_count) { if (i == 0){old_count = data;}
else{
alert('New Enquiry!');
old_count = data;}
} i=1;
}
});
},1000);
PHP Code:
include("config.php");
$sql = "SELECT count(*) as count FROM Enquiry";
$qry = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($qry);
echo $row['count'];
P.S. My first Stackoverflow answer.
Upvotes: 5
Reputation: 3679
I think your whole approach will not work. I suggest, PHP to only generate the count and JS to check if it has changed.
$sql = "SELECT * FROM i_case";
$qry = pg_query($connection, $sql);
$res = pg_num_rows($qry);
echo $res;
and in JS, you could do somesthing like this:
var count_cases = -1;
setInterval(function(){
$.ajax({
type : "POST",
url : "file.php",
success : function(response){
if (count_cases != -1 && count_cases != response) alert('new record on i_case');
count_cases = response;
}
});
},1000);
Upvotes: 2
Reputation: 160833
Your loop even won't run once because $res
is same with $res_1
in almost all cases. If the loop runs, it's a horrible infinite loop.
What you should do is like below.
The javascript:
var old_count = 0;
setInterval(function(){
$.ajax({
type : "POST",
url : "file.php",
success : function(data){
if (data > old_count) {
alert('new record on i_case');
old_count = data;
}
}
});
},1000);
And the PHP code:
$sql = "SELECT count(*) as count FROM i_case";
$qry = pg_query($connection, $sql);
$row = pg_fetch_assoc($qry);
echo $row['count'];
Upvotes: 6
Reputation: 6814
Have you checked the you're connected to db?
i mean $connection
variable returns true?, because i didn't found any code in php that connects to database.
If you could tell the what response you're getting from the server end, then i can help you bit more.
Upvotes: 1