0

I have a very basic question but dont know how to do it. I want to refresh a page if the row count in a mysql table increases. I have tried a few different things like adding a seperate column in the table with the value being (id + 1) if the amount of rows and this value were equal then to do the refresh. This worked but then it started refreshing every 5 seconds because the statement kept evaluating to true. All I really want is a simple

      if($num_rows changes){
 // do this
}

any and all help would be great thanks

5
  • Are you trying to refresh a client-side page when a server-side table changes? Commented Jun 19, 2013 at 19:42
  • yes. should I look into doing it a different way? Commented Jun 19, 2013 at 19:44
  • no, just wanted to make sure I understood correctly :) This being the case Havemonstret has already answered the question Commented Jun 19, 2013 at 19:46
  • Jimbo - what is the best programming language to change the content of a page for everyone that is currently on that exact page. ?? If 5 people are at index.com and one user submits something... I want them all to be able to see that. whats the best language for that? Commented Jun 19, 2013 at 20:40
  • I think what people have suggested. PHP server side and something like jQuery on the client side. Every user's page just checks for an event using an ajax call as Rotherford has outlined. When that event occurs their page refreshes. That way if a user submits something, all others will see it within their poll interval of the server Commented Jun 19, 2013 at 21:18

2 Answers 2

5

To check the number of affected rows of an INSERT, DELETE, UPDATE or REPLACE query you can use

$db->affected_rows();

If you wish to compare the number of rows to the number of rows from the past, you have to save the previous amount of rows, possibly in a session. It all depends on who performs the queries.

To get the total number of rows in a table:

SELECT COUNT(*) AS `numrows` FROM `table`;
Sign up to request clarification or add additional context in comments.

Comments

1

This will work: AJAX->PHP->MySQL

    $(document).ready(function() {
    setInterval("ajaxd()",10000);
});

function ajaxd() { 
  $.ajax({
   type: "GET",
   url: "new_rows.php",
   success: function(msg){
     if (msg.HasNewRows) {
       //new rows -> refresh
       document.location.reload(true);
     }
   }
 });
}

new_rows.php

<?php
session_start();
//perform query & num_rows() => $new

if ($new > $_SESSION['current']) {
 $_SESSION['current'] = $new;
 return json_encode(array("HasNewRows"=>true));
}else{#same with false}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.