0

I have an employees table here to see whether they are in the office or out. I have a php file to select the status from the database. I tried changing the status from out to in and in to out in the database but I cannot see any changes in my php page until I reload it. How can I get my php page change automatically when there is a change in the database?? I m a newbie so please help out... Thanks in advance. I have the php file here:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT Name,Status FROM Employees';

mysql_select_db('new');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "Name :{$row['Name']}  <br> ".
         "Status: {$row['Status']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>
Alok Naushad
  • 1,435
  • 2
  • 9
  • 9
  • 1
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Apr 07 '16 at 17:02
  • _How can I get my php page change automatically when there is a change in the database_ You cannot! Not without a lot more technology than you are ready for at the moment – RiggsFolly Apr 07 '16 at 17:04
  • 1
    if this is about a simple page refresh, then just do that. Many ways to go about it. However, if this is about triggering something, then that's another ballgame. – Funk Forty Niner Apr 07 '16 at 17:08

3 Answers3

0

HTTP is not a asynchronous protocol. The only way to refresh the data on the webpage is to reload the page eg;re-do the SQL query, parse and display the results. You could possibly do what you want using AJAX methods.. but, I do something like this for an "in-house" page to reload it every 5 minutes...

<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
<title>TITLE</title>
</head>
<body onload="JavaScript:timedRefresh(300000);">
<?php //do the query, display results ...
?>
</body>
</html>
Duane Lortie
  • 1,285
  • 1
  • 12
  • 16
0

If you really want this, you can do an ajax request every say 5 seconds, send the old values with the request and compare them to the database, if there is a difference you trigger a refresh, this way you only refresh your page when there is an actual update. (or you just update it using ajax every 5 seconds, looks a lot cleaner than a full page refresh)

Both require polling though, there is no easy way to do it without just checking every so many seconds.

Jester
  • 1,408
  • 1
  • 9
  • 21
-2

Use the below mysql

 while($row = mysql_fetch_array($retval))
    {
        echo "Name :{$row['Name']}  <br> ".
             "Status: {$row['Status']} <br> ".
             "--------------------------------<br>";
    }
Ajay
  • 235
  • 2
  • 8
  • I have changed the argument of myself_fetch_array($retval). There is no 2nd argument – Ajay Apr 08 '16 at 04:29