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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:23:19  来源:igfitidea点击:

how to fetch only a row from a mysql query

phpmysql

提问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 the whileloop 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_arraywithout 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 breakto 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 breakis not really elegant in this case).

您也可以组合这些方法(尽管break在这种情况下使用并不是很优雅)。

The best approach is using LIMITand omitting the whileloop, 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, Ywhere 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";

http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

http://php.about.com/od/mysqlcommands/g/Limit_sql.htm

回答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()正是你要找的。

http://php.net/mysqli_fetch_assoc

http://php.net/mysqli_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;

http://dev.mysql.com/doc/refman/5.5/en/select.html

http://dev.mysql.com/doc/refman/5.5/en/select.html

回答by KerrM