我需要不断地从我的数据库中读取每1秒,以获得最新的值。这里是我的代码:PHP常量SQL查询
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root','','Test');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT * FROM `Value`";
// perform the query and store the result
$result = $conn->query($sql);
if (!$result)
print 'Error!';
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
$output=$row;
print_r($output);
}
}
else {
print '0 results';
}
$conn->close();
?>
我的HTML代码刷新每1秒钟如下:
function reload(){
setInterval(getData,1000);
}
function getData()
{
$.get('test.php', function(data) {
var output = data;
document.getElementById("output").innerHTML = "Info: " + output;
});
}
....
<body onload="reload();">
<p id="output"></p>
</body>
一切工作正常,但大约5-10分钟的MySQL服务器崩溃后。我假设它正在超载。我的想法是,我每次连接每一秒都运行php脚本。我做错了吗?任何人都有更好的实施建议?
任何在你的错误日志
在mysqli的,你可以通过加入对前面加上主机名?/var/log/mysql/... – 2013-03-27 05:52:07
表'Value'中有多少行? – hjpotter92 2013-03-27 05:54:39
如果您每秒连接一次并断开连接可能会影响性能,则可以保持连接处于打开状态,这可能会有所帮助 – Bhadra 2013-03-27 05:54:47