简体   繁体   中英

How can I SELECT a single result from a mySQL database

I need to fetch data that is owned by each user. DB saves it with current user name and their data. Each user has many data rows. I need to get each users one row to be displayed every hour? so every one hour each users row1 changes to each users row2, when the rows finished it has to be loop again.

DB fields: 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 />";

You are missing '{'

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

May be PHP Cookie will help you in getting records as :- Your table must have a column 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));

 }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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