所以,这是我的代码:如何每隔几秒查询一次数据库?
<?php
//Connect to the database
include("config.php");
//Query the database and get the count
$result = mysql_query("SELECT * FROM ads");
$num_rows = mysql_num_rows($result);
?>
我怎样才能使它所以查询的数据库为计数每隔几秒钟?
所以,这是我的代码:如何每隔几秒查询一次数据库?
<?php
//Connect to the database
include("config.php");
//Query the database and get the count
$result = mysql_query("SELECT * FROM ads");
$num_rows = mysql_num_rows($result);
?>
我怎样才能使它所以查询的数据库为计数每隔几秒钟?
单独保存你的PHP脚本(我在下面的例子thescript.php)
<?php
//Connect to the database
include("config.php");
//Query the database and get the count
$q = mysql_query("SELECT count(*) as num FROM ads");
$count = mysql_fetch_result($q,0,'num');
echo $count;
?>
然后在您的home.php文件中使用AJAX/JavaScript的/ jQuery的:
<script>
$(function(){
function loadNum()
{
$('h1.countdown').load('thescript.php');
setTimeout(loadNum, 5000); // makes it reload every 5 sec
}
loadNum(); // start the process...
});
</script>
对不起,我不是一个非常有经验的程序员。所以,'thescript.php'应该是我上面给出的代码所在的文件(so,home.php)?另外,datacontainer DIV中需要什么?谢谢。 –
datacontainer div将从PHP脚本中加载内容。只要脚本将其加载到HTML代码中的现有元素中,就可以随意命名它......并且对于AJAX解决方案,只需将上述所有代码放在一个单独的文件中,我称之为它thescript.php,但你可以想到更聪明的东西! :) – jtheman
谢谢!我会尝试的。 –
你确定这是最好的解决方案?假设您想从浏览器/客户端执行此操作,则可能每隔几秒就会发出大量针对数据库的请求。 –
这取决于。你想用$ num_rows做什么?你把它扔掉了吗?显示它?给它发电子邮件? – slashingweapon
你为什么想要? – 2012-11-20 23:02:05