繁体   English   中英

如何从 mySQL 数据库中选择单个结果

[英]How can I SELECT a single result from a mySQL database

我需要获取每个用户拥有的数据。 DB 用当前用户名和他们的数据保存它。 每个用户有许多数据行。 我需要让每个用户每小时显示一行吗? 所以每隔一小时,每个用户的 row1 都会更改为每个用户的 row2,当行完成时,它必须再次循环。

数据库字段:id、名称、消息

$select=mysql_query("select * from commenttable where name='?'");
while($row=mysql_fetch_array($select))
echo "<div id='sty'>";
echo "<img src='files/fav icon.png'"."' width='50px' height='50px' align='left' />";
echo "<div id='nameid'>".$row['name']."</div>";
echo "<div id='msgid'>".$row['message']."</div>";
echo "</div><br />";

你缺少“{”

while($row=mysql_fetch_array($select))
{
echo "<div id='sty'>";
echo "<img src='files/fav icon.png'"."' width='50px' height='50px' align='left' />";
echo "<div id='nameid'>".$row['name']."</div>";
echo "<div id='msgid'>".$row['message']."</div>";
echo "</div><br />";
}

可能是 PHP Cookie 将帮助您获取记录:- 您的表必须有一个列 creationDate

<?php

if(!isset($_COOKIE['lastRecord']))
    $select=mysql_query("select * from commenttable where name='?' order by creationDate DESC limit 1");
else{
    $lastRecord=$_COOKIE['lastRecord'];
    $select=mysql_query("select * from commenttable where name='?' and creationDate < '{$lastRecord}'order by creationDate DESC  limit 1");
    unset($_COOKIE['lastRecord']);
}

while($row=mysql_fetch_array($select)){

 echo "<div id='sty'>";
 echo "<img src='files/fav icon.png'"."' width='50px' height='50px' align='left' />";
 echo "<div id='nameid'>".$row['name']."</div>";
 echo "<div id='msgid'>".$row['message']."</div>";
 echo "</div><br />";

 setcookie('lastRecord',$row['creationDate'],time() + (86400 * 30));

 }

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM