別のWebアプリケーションによって作成されたデータベースの変更を表示する一種のモニターを作成しています。変更を表示するために10秒ごとにページをリロードしていましたが、それは「方法」ではないと思います。私はいくつかの調査を行い、変更をチェックする関数を呼び出すことでajaxがそれを実行できることがわかったので、コードを書きましたが、何かが不足しているため、専門家に電話しています。主なアイデアは、ページがロードされたときに挿入されて $resultado という変数に保存された最後の行の id(number) 次に、同じクエリを実行して結果をチェックする check_changes という関数を呼び出します。両方の変数が等しい場合、変更はありません。リロードページが異なります。
モニター.php
<head>
<script type="text/javascript" src="./js/prototype.js"></script>
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
</head>
<?
//code that makes and print the query.
// here i check the last id(number) and saved on $resultado variable
$qry0="SELECT emergencia.id FROM emergencia ORDER BY emergencia.id DESC LIMIT 0,1";
$qry1=db_query($qry0,$conn);
while($row=$qry1->fetch_array())
{
$resultado=$row['id'];
}
//here i call check_changes every 30 seconds
echo "<script>setTimeout('check_changes()',30);";
//ajax function that check the status of database
echo "function check_changes(){
$.ajax({
type: 'POST',
data: {'data':$resultado}, //here am sending the value of result via post
url: './checker.php', //to checker.php
success: function(data) {
if(data.result==true){
window.location = window.location.pathname;
}
}
})
}";
echo "</script>";
?>
checker.php
<?
$result4 = $_POST['data'];
include( "/home/ocelas/proyecto/include/inter_dbc_innodb.php" );
$dbc=connect_db("seguridad");
// here i check the last id(number) and saved on $result3 variable
$qry0="SELECT emergencia.id FROM emergencia ORDER BY emergencia.id DESC LIMIT 0,1";
$qry1=$dbc->query($qry0);
while($row=$qry1->fetch_array())
{
$result3=$row['id'];
}
//here i check if both are equal send false to monitor.php
if ($result4==$result3){
$result=false
echo json_encode($result);
}
else {
// a new row has been inserted, send true to monitor.php an reload page
$result=true
echo json_encode($result);
}
?>