简体   繁体   中英

How I can Get value of nth row from mysql?

I tried below code

$sql=mysql_query("SELECT * FROM `user`");
$result=mysql_result($sql, 6);
echo $result;

It returns the value of first field of 7th row. But Its works fine till 9th row. And after that it returns again from 1 . So anyone please help .. how I can get value of n-th row from mysql.

Thanking You.

sumansjournal@gmail.com

You can specify that from your query too using the LIMIT clause:

$sql=mysql_query("SELECT * FROM `user` limit 6, 1");

This will give you 7th record and thus you can specify any number there.

Be aware that you aren't guaranteed the order will be reliable unless you actually define how the rows should be ordered, using an ORDER BY clause. You're probably getting the rows back in the order they were inserted into the table. There's numerous things that can cause this "default order" to change. It's a really bad idea unless you really understand whats going on under the hood.

Here's an example that sorts the records by firstname. This probably isn't a good column to order the rows by, but we don't know enough about your table schema or requirements to suggest one.

$result = mysql_query("SELECT * FROM `user` order by firstname desc limit 6,1");
print_r(mysql_fetch_assoc($result));
$i=6;     
if (!mysql_data_seek($sql, $i)) {
       echo "Cannot seek to row $i: " . mysql_error() . "\n";
}

then use any traditional method like mysql_fetch_array

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