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)
这通常是怎么做的? (我只是想避免不必要的循环)
9 个解决方案
#1
11
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:如果添加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;
-
If you call
mysql_fetch_array
without a loop, you will get the first row of the result set:如果在没有循环的情况下调用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;
-
If you add
break
to the loop body, the loop will terminate after one iteration.如果向循环体添加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在他的回答中所示。这使代码更清晰,并避免了数据库中不必要的操作。
#2
8
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);
#3
3
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";
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
#4
2
select whatIwant FROM table where id = 'myId' limit 1
#5
2
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.
这只会获取第一行。
#6
2
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
#7
0
I think you're looking for this:
我想你正在寻找这个:
http://php.net/manual/en/function.mysql-fetch-row.php
http://php.net/manual/en/function.mysql-fetch-row.php
#8
0
select whatIwant FROM table where id = 'myId' LIMIT 1;
选择whatIwant FROM table,其中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
#9
0
$sql224="select * from z_category order by id asc LIMIT 1";
$res224=mysql_query($sql224);
$rw224=mysql_fetch_array($res224);
$activeid2= $rw224['pkid'];
#1
11
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:如果添加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;
-
If you call
mysql_fetch_array
without a loop, you will get the first row of the result set:如果在没有循环的情况下调用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;
-
If you add
break
to the loop body, the loop will terminate after one iteration.如果向循环体添加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在他的回答中所示。这使代码更清晰,并避免了数据库中不必要的操作。
#2
8
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);
#3
3
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";
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
#4
2
select whatIwant FROM table where id = 'myId' limit 1
#5
2
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.
这只会获取第一行。
#6
2
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
#7
0
I think you're looking for this:
我想你正在寻找这个:
http://php.net/manual/en/function.mysql-fetch-row.php
http://php.net/manual/en/function.mysql-fetch-row.php
#8
0
select whatIwant FROM table where id = 'myId' LIMIT 1;
选择whatIwant FROM table,其中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
#9
0
$sql224="select * from z_category order by id asc LIMIT 1";
$res224=mysql_query($sql224);
$rw224=mysql_fetch_array($res224);
$activeid2= $rw224['pkid'];