plz i want to ask a question in select statment i want to select the last 3 entries in the table products i tried a code but it selects all the entries could u help me plz <? $conn=mysql_connect("localhost","root","root"); mysql_select_db('project3'); $sql="select * from product ORDER BY id DESC"; $rs=mysql_query($sql); $i=1; while ($data=mysql_fetch_array($rs)) { if (i<3) { echo $data['id'].$data['name']."<br>"; } $i++; } ?> Code (markup):
You can also change the sql: $sql="select * from product ORDER BY id DESC"; to $sql="select * from product ORDER BY id DESC LIMIT 3"; this returns only 3 records - not a big issue on a small database, but if you have a large number of product ids (id) it could return a lot of data.
in your select statement you can get specific as well. Something like $sql = "SELECT name, address, email from product ORDER BY id DESC LIMIT 3"; That works as well.
My pleasure - and as IGiveMoney mentioned, you can reduce the # fields for each record - great if you have very long fields that don't need to be read in (like a large product description, for example).