Please Help Me Selecting MySQL Top Row
Started on the old forum in November 2013 · view the original in the archive · old links redirect here
Rockto wrote: My database table name is users and colums are id,name,mobile,facebook.... Now i want to display first 10 members data.... How can i do that? Please give me tested and working code.... If possible give me a full php file.... I badly need it......
try this for example sorry if i wrong
gammz wrote: try this for example sorry if i wrong
Thanks for replay and file...i gonna test it...
Here you go.
select * from users limit 10
The
*
means select all columns in the table, and the
limit 10
means only return the first 10 rows/records in the result
Depending on what MySQL version you are using you will typically use the code as follows.
OLD SQL:
$sql = 'select * from users limit 10';
$query = mysql_query($sql);
while ($row = $query)
{
$row['username'] //Just insert your column name you want
}
PDO:
$sql = 'select * from users limit 10';
$query = $db->query($sql);
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$row['username']; //Just insert your column name you want
}
Say Thanks If you Like
Hello Brother If you have the field name id in your table then use this code
SELECT * FROM table_name ORDER BY id DESC LIMIT 0,10 <- This is for last 10 fields
SELECT * FROM table_name ORDER BY id ASC LIMIT 0,10 <- This is for First 10 fields