php 如何从mysql查询中只获取一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9657807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fetch only a row from a mysql query
提问by Toni Michel Caubet
i allways end up doing this
我总是这样做
$q = "select whatIwant FROM table where id = 'myId'";
$r = mysql_query($q);
while($i = mysql_fetch_array($r){
/* iterate just one withem */
$j = $i['whatIwant'];
}
echo $j;
how is this usually done? (i just want to avoid the unecessary loop)
这通常是怎么做的?(我只是想避免不必要的循环)
回答by Felix Kling
In addition to the correct answers, there are multiple ways to handle this:
除了正确答案外,还有多种方法可以解决这个问题:
If you add
LIMIT 1
, the result set will only contain one row and thewhile
loop will terminate after one iteration:$q = "select whatIwant FROM table where id = 'myId' LIMIT 1"; $r = mysql_query($q); while($i = mysql_fetch_array($r)) { $j = $i['whatIwant']; } echo $j;
If you call
mysql_fetch_array
without a loop, you will get the first row of the result set:$q = "select whatIwant FROM table where id = 'myId'"; $r = mysql_query($q); $i = mysql_fetch_array($r); $j = $i['whatIwant']; echo $j;
If you add
break
to the loop body, the loop will terminate after one iteration.$q = "select whatIwant FROM table where id = 'myId'"; $r = mysql_query($q); while($i = mysql_fetch_array($r)) { $j = $i['whatIwant']; break; } echo $j;
如果添加
LIMIT 1
,结果集将只包含一行,while
循环将在一次迭代后终止:$q = "select whatIwant FROM table where id = 'myId' LIMIT 1"; $r = mysql_query($q); while($i = mysql_fetch_array($r)) { $j = $i['whatIwant']; } echo $j;
如果
mysql_fetch_array
不循环调用,将得到结果集的第一行:$q = "select whatIwant FROM table where id = 'myId'"; $r = mysql_query($q); $i = mysql_fetch_array($r); $j = $i['whatIwant']; echo $j;
如果添加
break
到循环体,循环将在一次迭代后终止。$q = "select whatIwant FROM table where id = 'myId'"; $r = mysql_query($q); while($i = mysql_fetch_array($r)) { $j = $i['whatIwant']; break; } echo $j;
You can also combine these approaches (although using break
is not really elegant in this case).
您也可以组合这些方法(尽管break
在这种情况下使用并不是很优雅)。
The best approach is using LIMIT
and omitting the while
loop, as @zaf shows in his answer. This makes the code clearer and avoids unnecessary operations in the database.
最好的方法是使用LIMIT
和省略while
循环,正如@zaf 在他的回答中所示。这使得代码更清晰,避免了数据库中不必要的操作。
回答by zaf
You can specify the number of rows in the sql query using the 'LIMIT' syntax.
您可以使用 'LIMIT' 语法指定 sql 查询中的行数。
Also, you can just remove the while loop and get the first row returned - if thats all you want.
此外,您可以删除 while 循环并返回第一行 - 如果这就是您想要的。
For example (without returned value checking):
例如(没有返回值检查):
$q = "select whatIwant FROM table where id = 'myId' LIMIT 1";
$r = mysql_query($q);
$i = mysql_fetch_array($r);
print_r($i);
回答by Joseph
add LIMIT X, Y
where X is your starting row and Y is the number of rows to return
添加LIMIT X, Y
其中 X 是您的起始行,Y 是要返回的行数
//will always return 1 row. note your orders
$q = "SELECT whatIwant FROM table WHERE id = 'myId' limit 0, 1";
回答by Nobody
Well then leave out the loop:
那么离开循环:
$q = "select whatIwant FROM table where id = 'myId'";
$r = mysql_query($q);
$i = mysql_fetch_array($r);
$j = $i['whatIwant'];
echo $j;
This will only fetch the first line.
这只会获取第一行。
回答by juergen d
select whatIwant FROM table where id = 'myId' limit 1
回答by Arda
mysql_fetch_assoc()
is exactly what you're looking for.
mysql_fetch_assoc()
正是你要找的。
回答by abin
$sql224="select * from z_category order by id asc LIMIT 1";
$res224=mysql_query($sql224);
$rw224=mysql_fetch_array($res224);
$activeid2= $rw224['pkid'];
回答by Sean
select whatIwant FROM table where id = 'myId' LIMIT 1;
select whatIwant FROM table where id = 'myId' LIMIT 1;