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

时间:2015-06-27 03:57:36

标签: php mysql sql database

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

数据库字段:id,name,message

$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 />";

2 个答案:

答案 0 :(得分:0)

你缺少'{'

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 />";
}

答案 1 :(得分:0)

可能是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));

 }

?>