我希望每次鼠标移动时都从MySql数据库中获取值。我为此使用了Javascript和Php。
的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代码):
$(window).mousemove(function( event ) {
document.getElementById("nindicator").innerHTML="<b><?php echo dbcount();?></b>";
//alert("working");
});
Php功能代码:
//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(*)"];
}
所以这里dbcount()获取值并首次显示它。之后,当再次执行函数时,它只显示第一个值,甚至不检查数据库。希望我澄清了我的问题。请回去。