简体   繁体   English

SQL查询仅显示第一个结果

[英]SQL query only display first result

the code below display the row 116 twice, it won't display row 118. Any idea how to solve this issue? 下面的代码两次显示第116行,但不会显示第118行。知道如何解决此问题吗?

$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')")
        or die(mysql_error()); 
$info = mysql_fetch_array($data); 

foreach ($info as $item) {
  echo($item); 
}

mysql_fetch_array only fetches a single row. mysql_fetch_array仅获取一行。 Typically it is used in a while loop to cycle through all the results. 通常,它在while循环中用于循环浏览所有结果。

To continue your example above: 继续上面的示例:

$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')") or die(mysql_error()); 
while ($item = mysql_fetch_array($data)) {
    echo($item)
}

your query must be as: 您的查询必须为:

$data = mysql_query("SELECT * FROM item WHERE dcid IN('116','118')") or die(mysql_error()); 
while ($item = mysql_fetch_array($data)) {
    echo $item['column_name1'];
}   echo $item['column_name2'];

mysql_fetch_array returns a single row in an array like this that contains both an associative array and a regular numeric-keyed result set of your row. mysql_fetch_array在这样的数组中返回一行,该行既包含关联数组,又包含该行的常规数字键结果集。

0=> column,
column=>column

Thats why it returns twice in foreach.Use it like this 这就是为什么它在foreach中返回两次的原因

mysql_fetch_array($result, MYSQL_ASSOC);

Also, if dcid is your key and it's autoincrementing ( ID for rows ) you can also use LIMIT 116,118. 另外,如果dcid是您的密钥,并且它是自动递增的(行的ID),则还可以使用LIMIT 116,118。

For more info: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm 有关更多信息: http : //php.about.com/od/mysqlcommands/g/Limit_sql.htm

use group by as this 使用group by

$data =mysql_query("SELECT * FROM item WHERE dcid IN('116','118') group by dcid")
       or die(mysql_error());

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

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