Mysql query result does not give me correct result

I am refreshing my php page every 5 seconds

window.setInterval(function(){ 
    console.log(<?=mysql_num_rows(mysql_query("select * from records"))?>)
}, 5000);

      

If I have 5 records in my table the output is 5, but when I insert a new record the output remains 5, if I haven't refreshed the whole page then it will be 6 ..

What's wrong?

+3


source to share


4 answers


This is because you mix PHP with JS, the PHP value is written on page load, and it remains that you stay on the page all the time.

You should be using an Ajax request that asks for the value again.

Make a PHP page that only returns this value:

<?=mysql_num_rows(mysql_query("select * from records"))?>

      



Then add JS something like this:

    setInterval($.ajax({
        url: 'your-url-with-php-result', 
        type: 'GET', 
        cache: false,
        success: function(result){                          
              console.log(result);
        }           
    }), 5000);

      

You also need to include JQuery for this

0


source


You have a fundamental misunderstanding of how PHP and Javascript work. Don't worry, this is very common when first learning web development!

When you use PHP to print to a page (like the code inside <?= ?>

), the code is executed on the server . Try looking at the source of your page and you will see that when the browser receives the page, the PHP code has already been replaced with its result.



This means that when the Javascript loop is executed, it simply repeats the same pre-computed value.

To accomplish what you are about to do, you will either need to simply refresh the page every 5 seconds, or read the AJAX. AJAX is how you can load new content from the server (so whatever from the database) without reloading the page. This is what StackOverflow uses to display "1 new answer to this question", for example.

+1


source


you cannot use php in javascript codes

you can store php in example.php and you call example.php in javascript code

your example.php like this

<?php
    echo mysql_num_rows(mysql_query("select * from records"));
?>

      

and now you can get the data from the example in about 5 seconds with this code ( load is a jquery function )

<html>
 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  <script>
    setTimeout(function(){
     $("#showRes").load("example.php"); 
    }, 5000);
  </script>
 </head>
 <body>
  <div id="showRes"></div>
 </body>
</html>

      

0


source


you usually mix jaavscript and php concept. you can solve this very easily. passing php value to ajax and that u value should depend on that javascript. Look at the source code of your page and you will see that when the browser receives the page, the PHP code has already been replaced with its result.

write php code in php page like: -

    <?=mysql_num_rows(mysql_query("select * from records"))?>

      

and use js like another page. call this value as your requirement. as: -

     setInterval($.ajax({
     url: 'your-url-with-php-result', 
     type: 'GET', 
     cache: false,
     success: function(result){                          
          console.log(result);
     }           
     }), 5000)

      

If you need jquery then you include that in your code as well.

0


source







All Articles