我正在尝试创建一个页面,从顶部再次读取我的代码,以检查我的数据库中有多少数据。如果它达到10,那么我想执行一些事情,例如" truncate table"。
这是我现在的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function()
{
function loadlink()
{
$.ajax({
type: "POST",
url: "load.php",
data: { action: 'pass any value if required' },
success: function(theResponse) {
// optional
$('#displayMessage').html(theResponse);
}
});
}
loadlink(); // This will run on page load
setInterval(function(){
loadlink() // this will run after every 10 seconds
}, 500);
});
</script>
<div id="displayMessage">Result:<br></div>
<?php
error_reporting(0);
include('core/dbconnection.php');
#$action = isset($_POST['action']) ? $_POST['action'] : ''; // optional
#$action = 'Power';
$sql = mysql_query("SELECT * FROM tb_staff");
$count = mysql_num_rows($sql);
if($count <= '4' ){
//backflip
echo "backflip";
}else{
//wait10seconds,check again
echo "Not 4";
}
?>
但它反复出现两次
输出:
结果: 不是4 不是4
答案 0 :(得分:0)
<强> load.php 强>
<?php
$action = isset($_POST['action']) ? $_POST['action'] : ''; // optional
$sql = mysql_query("SELECT * FROM tb_x");
$count = mysql_num_rows($sql);
if($count = 10){
//backflip
}else{
//wait10seconds,check again
}
?>
主文件
ajax将在每10秒后运行一次,以调用load.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
function loadlink()
{
$.ajax({
type: "POST",
url: "load.php",
data: { action: 'pass any value if required' },
success: function(theResponse) {
// optional
$('#displayMessage').html(theResponse);
}
});
}
loadlink(); // This will run on page load
setInterval(function(){
loadlink() // this will run after every 10 seconds
}, 10000);
});
</script>
<div id="displayMessage"><!--optional--></div>
注意:
现在不推荐使用原始MySQL扩展,并且在连接到数据库时会生成E_DEPRECATED错误。而是使用MySQLi或PDO_MySQL扩展。
MySQLi教程:https://www.w3schools.com/PhP/func_mysqli_connect.asp