简体   繁体   English

如何从mysql获取第n行的值?

[英]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. 它返回第7行第一个字段的值。 But Its works fine till 9th row. 但是它可以正常工作到第9行。 And after that it returns again from 1 . 之后,它又从1返回。 So anyone please help .. how I can get value of n-th row from mysql. 所以任何人都请帮忙..我怎样才能从mysql获取第n行的值。

Thanking You. 感谢您。

sumansjournal@gmail.com sumansjournal@gmail.com

You can specify that from your query too using the LIMIT clause: 您也可以使用LIMIT子句从查询中指定:

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

This will give you 7th record and thus you can specify any number there. 这将为您提供第7条记录,因此您可以在此处指定任何数字。

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. 请注意,除非您实际使用ORDER BY子句定义行的排序方式,否则不能保证顺序会可靠。 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 然后使用任何传统方法,例如mysql_fetch_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM