tags:

views:

346

answers:

3

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.

[email protected]

A: 
$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

Pentium10
+3  A: 

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.

Sarfraz
How you can sure that Id will flow in a order like 1>2>3>...50>51>...It can also break order ... then ??
Suman Biswas
+1  A: 

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));
chris
Why you are thinking that Id in an order !! If I delete any row it will break the order .. and the errors will come
Suman Biswas