Adam Smith
Adam Smith

Reputation: 145

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?

$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); 
}

Upvotes: 0

Views: 370

Answers (5)

echo_Me
echo_Me

Reputation: 37233

use group by as this

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

Upvotes: 0

Nutic
Nutic

Reputation: 1437

Also, if dcid is your key and it's autoincrementing ( ID for rows ) you can also use LIMIT 116,118.

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

Upvotes: 0

Mihai
Mihai

Reputation: 26784

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.

0=> column,
column=>column

Thats why it returns twice in foreach.Use it like this

mysql_fetch_array($result, MYSQL_ASSOC);

Upvotes: 1

Dinesh
Dinesh

Reputation: 4110

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'];

Upvotes: 1

cbeckner
cbeckner

Reputation: 1818

mysql_fetch_array only fetches a single row. Typically it is used in a while loop to cycle through all the results.

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)
}

Upvotes: 2

Related Questions