每 5 秒调用一次 MySQL 查询


call mysql query each 5 seconds

我正在尝试根据时间农场自动调用MySQL查询内容以将其显示为一行显示MySQL查询的内容

我能够制作计时器函数,但我无法将其与 ajax 函数一起植入以自动调用 MySQL 查询

我需要的是将我在 loop() 函数中制作的变量放在 ajax 函数中showUser(str)

这是我使用 JavaScript 和 Ajax 代码的 HTML:

<html>
<head>
<script>

var x=0;
function loop() {
    x= x+1;
if (x == 100) x = 1;
document.getElementById("timevar").innerHTML=x;    
setTimeout("loop()", 5000);     
}    loop(); 
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true); '' timevar should be instead of str
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>

timevar 应该代替 str 或将其值赋予函数showUser

这是页面上名为getuser的PHP代码.php:

<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Comments</th>
<th>Clicks</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Comments'] . "</td>";
  echo "<td>" . $row['Clicks'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
mysql_close($con);
?>

我正在尝试做的是使每个带有其信息的名称在同一行出现和翻转..我不希望它们出现在表格中..我试图使其更具动态性

任何帮助表示赞赏..非常感谢

你可能会发现使用 jQuery 来处理你的 ajax 调用可能会让你的生活更轻松。

http://api.jquery.com/jQuery.get/

    $.get('ajax/callmySQL.php');

你真的应该看看jQuery。它使生活变得轻松得多。请参阅 http://api.jquery.com/jQuery.get/

jQuery的$.get在请求完成时调用回调。您只需从它们更新文档并为下一个请求设置计时器。

像这样:

function update() {
    $.get(url, null, function(data, textStatus, jqXHR) {
            $('#txtHint').html(data);
            setTimeout(update, 5000);
        });
}
setTimeout(update, 5000);

顺便说一句,不需要将代码作为字符串传递,您可以直接输入函数名称:setTimeout(loop, 5000);

我找到了我想要的东西......这只是因为我慢慢学习并且不熟悉JavaScript:)

我一直有答案,但我不知道怎么写,现在我知道:)

我定义了我知道它应该存在的变量timevar.现在我通过将其放入函数中来定义它,因为每 10 秒增加数字 go +1,然后在间隔 10 秒后再次将其放入循环中

我想我写了太多我不需要的东西......但毕竟它的工作找到了:)

这是答案,也许它会在某个时候帮助某人

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true); // timevar should be instead of str
xmlhttp.send();
}
var setdelay=10000 //delay 10 seconds
var timevar=0;
function moveit(how){
if (how==1){ //cycle foward
if (timevar<100)
timevar++
else
timevar=0
}
else{ //cycle backward
if (timevar==0)
timevar=100
else
timevar--
}}
setInterval("moveit(1)",setdelay)
var x=0;
function loop() {
    x= x+1;
if (x == 100) x = 1;
showUser("timevar");
setTimeout("loop()", 10000);     
}    loop();
</script>
</head>
<body onload="loop()">
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>