jQueryとPHPを使用してmysqlから通知を取得するための簡単なスクリプトを作成しようとしています(私のコードは以下です)。現時点では、jquery は毎秒 loadmore.php を取得し、それを含む div を更新します。サイトに大量のトラフィックが発生した場合、サーバーに過度の負荷がかかるのではないかと思っていました。
ソースコード: index.php -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#notifications').load('loadmore.php');
}, 1000); // refresh every 10000 milliseconds
</script>
<body>
<div id="notifications"> </div>
</body>
</html>
loadmore.php -
<?php
include('config.php');
?>
<?php
$sql=mysql_query("select * from updates ORDER BY item_id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['item_id'];
$message=$row['item_content'];
?>
<?php echo $message; ?><br />
<?php } ?>