在我的php站点上,我想从使用javascript的mysql数据库中每三秒钟检索一次数据。无法通过javascript函数从数据库获取php数据
问题:当我使用SELECT * from msgtable
检索数据时,则php和javascript startTime
似乎都不起作用。
的JavaScript:
setInterval(function() {
var link = document.getElementById("chg");
link.href = "http://google.com.pk";
link.innerHTML = "<?php dynamic(); ?>";
}, 3000);
function startTime() {
var today = new Date();
var s = today.getSeconds();
s = checkTime(s);
if(s == s+3) { alert("faraz"); }
document.getElementById('time').innerHTML= s;
t = setTimeout(function() { startTime() }, 500);
}
function changeURL() {
var link = document.getElementById("chg");
link.href = "http://google.com.pk";
link.innerHTML = "Google Pakistan";
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
PHP:
<?php
$connection = mysql_connect("localhost","root","");
$db_select = mysql_select_db("msgs",$connection);
$result = mysql_query("SELECT * FROM msgtable", $connection);
function dynamic() {
echo "faraz";
while ($row = mysql_fetch_array($result)) {
echo $row['msgBody'] ;
}
}
?>
HTML:
<body onLoad="startTime()">
<div id="chg1"> 3 Seconds to Google Pakistan </div>
<a href="http://google.it" id="chg">Google Italia</a>
<!-- Hafiz Faraz Mukhtar-->
<div id="time"> Time </div>
<div class="publicOut">Faraz</div>
</body>
相关http://stackoverflow.com/q/13840429/1331430 –