Php 更改轮询数据库-Ajax/jQuery
我有个问题。我正在创建一个拍卖网站,比如BidCompetiers之类的 我目前正在使用autcion脚本本身及其完全工作,但我必须始终每隔5秒重新加载页面,以检查是否有新的出价,在我看来,这不是一个好方法 因此,我正在优化代码,以便页面仅在有新出价时刷新 目前我有以下代码: poll.phpPhp 更改轮询数据库-Ajax/jQuery,php,jquery,mysql,ajax,database,Php,Jquery,Mysql,Ajax,Database,我有个问题。我正在创建一个拍卖网站,比如BidCompetiers之类的 我目前正在使用autcion脚本本身及其完全工作,但我必须始终每隔5秒重新加载页面,以检查是否有新的出价,在我看来,这不是一个好方法 因此,我正在优化代码,以便页面仅在有新出价时刷新 目前我有以下代码: poll.php include('../configs/db_ligar.php'); // db connection stuff.. $query = "SELECT auction_id, au
include('../configs/db_ligar.php'); // db connection stuff..
$query = "SELECT auction_id, auction_bid_counter FROM leilao WHERE auction_product_id = $id_prod";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
echo -1;
} else {
$resultArray = array();
$row = mysql_fetch_array($result);
$resultArray['auction_id'] = $row['auction_id'];
$resultArray['auction_bid_counter'] = $row['auction_bid_counter'];
//Fast fix ... not pretty :)
echo "[" . json_encode($resultArray) . "]";
}
index.php
$(document).ready(function()
{
var checkTable = setInterval(function(){
$.post('poll.php', function(resultData){
//If there is data
if(!resultData == -1){
//DIFFERENT: need to get the first element from the eval array
var resultObject = eval(resultData)[0];
alert('auction_id: ' + resultObject.auction_id + ' - auction_bid_counter: ' + resultObject.leilao_bid_counter);
}
}, 1000);
});
});
此代码不正确吗?我觉得代码似乎不正确。。。您遇到了什么问题?因此我手动更改了phpmyadmin中的拍卖\出价\计数器值,并弹出一个消息框,对吗?嗯,它不是:sHmm,将
if(!resultData==-1){
语句注释掉一秒钟,并用alert(resultData)替换它;看看它会产生什么…你不需要为post结果设置一个数据类型吗?就像你的JSON一样…哈哈,我找到了。index.php脚本没有特定的语言脚本!它就像*code*EDIT:它不起作用,但当我提醒(resultData)时,它会显示如下内容:{“拍卖id”:“1”,“拍卖出价计数器”:“5”}哈哈,很好,你超越了这一点…数据结果正是你想要的,我猜,这是一个JSON结果。。。