每5秒调用一次mysql查询。

时间:2022-11-30 03:58:18

i am trying to call mysql query contents automatically based on time farm to display it as one line show the contents of mysql query

我正在尝试基于time farm自动调用mysql查询内容,将其显示为一行显示mysql查询内容

i was able to make the timer function but i couldn't implant it with the ajax function to call the mysql query automatically

我可以设置定时器函数,但是我不能用ajax函数来自动调用mysql查询

what i need is put the variable which i made in the loop() function in the ajax function showUser(str)

我需要的是将我在loop()函数中创建的变量放到ajax函数showUser(str)中

here is my html with javascript and ajax code :

这是我的html与javascript和ajax代码:

<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 should be instead of str or giveing it's value to showUser function

timevar应该代替str,或者将其赋值给showUser函数。

here is php code on the page which called getuser.php :

这里是在页面上调用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);
?>

what i am trying to do is making each name with it's info to appear and flip at the same line .. i don't want them to appear in a table .. am trying to make it more dynamic

我要做的是用它的信息创建每个名字,并在同一条线上翻转。我不希望他们出现在桌子上。想让它更有活力吗

any help appreciated .. thank you so much

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

3 个解决方案

#1


2  

You might find that using jQuery to handle your ajax calls may make your life easier.

您可能会发现,使用jQuery处理ajax调用可能会使您的工作更轻松。

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

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

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

#2


1  

You really should take a look on jQuery. It makes life a lot easier. See http://api.jquery.com/jQuery.get/

你真的应该看看jQuery。它让生活变得更容易。参见http://api.jquery.com/jQuery.get/

jQuery's $.get calls callbacks when request is done. You simply update document from them and setup timer for next request.

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

Something like:

喜欢的东西:

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

Btw, there is no need for passing code as string, you can enter function name directly: setTimeout(loop, 5000);

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

#3


0  

i found what i was looking to .. it was just because am slowly learning and new to javascript :)

我找到了我要找的东西。这只是因为我在慢慢地学习,而且对javascript还很陌生:)

i had the answer all the time but i didn't knew how to write it and now i knew :)

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

i define the variable timevar which i knew it should be there .. now i define it by putting it in a function as increasing number go +1 each 10 seconds then i put it again in the loop after interval for 10 seconds

我定义了变量timevar我知道它应该在那里。现在我将它定义为每10秒增加1个数然后在间隔10秒后将它再次放入循环中

i think i write too many things which i don't need there .. but after all it works find :)

我想我写了太多我不需要的东西。但不管怎么说,找到了:

here is the answer maybe it helps someone somewhere sometime

这就是答案,也许它在某些时候对某些人有所帮助

<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>

#1


2  

You might find that using jQuery to handle your ajax calls may make your life easier.

您可能会发现,使用jQuery处理ajax调用可能会使您的工作更轻松。

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

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

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

#2


1  

You really should take a look on jQuery. It makes life a lot easier. See http://api.jquery.com/jQuery.get/

你真的应该看看jQuery。它让生活变得更容易。参见http://api.jquery.com/jQuery.get/

jQuery's $.get calls callbacks when request is done. You simply update document from them and setup timer for next request.

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

Something like:

喜欢的东西:

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

Btw, there is no need for passing code as string, you can enter function name directly: setTimeout(loop, 5000);

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

#3


0  

i found what i was looking to .. it was just because am slowly learning and new to javascript :)

我找到了我要找的东西。这只是因为我在慢慢地学习,而且对javascript还很陌生:)

i had the answer all the time but i didn't knew how to write it and now i knew :)

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

i define the variable timevar which i knew it should be there .. now i define it by putting it in a function as increasing number go +1 each 10 seconds then i put it again in the loop after interval for 10 seconds

我定义了变量timevar我知道它应该在那里。现在我将它定义为每10秒增加1个数然后在间隔10秒后将它再次放入循环中

i think i write too many things which i don't need there .. but after all it works find :)

我想我写了太多我不需要的东西。但不管怎么说,找到了:

here is the answer maybe it helps someone somewhere sometime

这就是答案,也许它在某些时候对某些人有所帮助

<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>