0

I want to fetch values from MySql database every time when mouse moves. I used Javascript and Php for this.

config.php

<?php
$conn = mysqli_connect('localhost','root','','info_schema');
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

Javascript (AJAX Code):

$(window).mousemove(function( event ) {
document.getElementById("nindicator").innerHTML="<b><?php echo dbcount();?></b>";
//alert("working");
});

Php function code :

//Database Count Function
function dbcount(){
global $conn,$temp;
$result3;
$result3=mysqli_query($conn,"Select count(*) from message_and_notifications.notifications where My_id=".$_SESSION['My_id']." and Mark_read=0");
$row=mysqli_fetch_array($result3);
return $row["count(*)"];
}

So here dbcount() gets value and displays it for first time. After that when function is executed again it simply displays the first value without even checking the database. Hope I clarified my question. Please get back.

  • "I want to fetch values from MySql database every time when mouse moves. I used Javascript and Php" - don't do it. This will take tremendous amount of resources. – user4035 May 22 '16 at 14:37
  • You need to write an Ajax query. – user4035 May 22 '16 at 14:38
  • 2
    You should at the very least have a delay, so you don't fire 100 requests if the user moves the cursor. I have a strong feeling this is a [XY problem](http://meta.stackexchange.com/a/66378) though. It seems you're only updating the unread notifications. Which you can do on a poll interval instead of messing with mouse move events. – JimL May 22 '16 at 14:39
  • Example? How to write it – Krishna Raj K May 22 '16 at 14:39
  • Here is an example: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – user4035 May 22 '16 at 14:41
  • @KrishnaRajK What part of the so-called AJAX code makes it AJAX, exactly?! And you do know that `` gets executed *once*, right? Setting `innerHTML` to the same value over and over doesn't really do anything. – Biffen May 22 '16 at 14:42
  • Yp its not ajax. Just javascript – Krishna Raj K May 23 '16 at 04:08

0 Answers0