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

4 replies · 344 views · started 13 years ago · 1 min read

4 participants
Rockto Newbie · 4 posts #1

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......

gammz Junior · 24 posts #2

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

Attachments: example.zip
Rockto Newbie · 4 posts #3

gammz wrote: try this for example sorry if i wrong

Thanks for replay and file...i gonna test it...

phoenix90 Developer · 113 posts #4

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 
}
Lue_chin Developer · 107 posts #5

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

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close