Derek Lim
Derek Lim

Reputation: 23

mysql query select multiple rows where column value is same

i have this table

Column_1        Column_2
1                 value1
2                 value1
3                 value2

My php query is

$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
print_r($query);

This returns only the 1st row. I am looking to display row 1 and 2. When I run the SQL in phpmyadmin it returns row 1 and 2. However, the php script only returns row 1... I also did an

echo count($query); 

But it returns only 1. What am i doing wrong?

Upvotes: 1

Views: 11616

Answers (3)

hoangvu68
hoangvu68

Reputation: 865

$query = "SELECT * FROM `table` WHERE `Column_2` = 'value1' ";
$res = mysql_query($query);
if(mysql_num_rows($res)!=0) {
    while($rowData = mysql_fetch_array($res)) {
        var_dump($rowData);
    }
}
  • Use mysql_num_rows to count number of results.
  • Use mysql_fetch_array or mysql_fetch_assoc to fetch data.

Upvotes: 2

Vishwanath Tadahal
Vishwanath Tadahal

Reputation: 116

Use mysql_fetch_array() function
 $query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
 $res = mysql_query($query);
 while($row = mysql_fetch_array($res))
   {
       echo $row['Column_1'];
       echo $row['value_1'];
   }

Upvotes: 0

Alexey Palamar
Alexey Palamar

Reputation: 1430

$query = "SELECT * FROM `table` WHERE `Column_1` = 'value1' ";
$res = mysql_query($query);
while($row = mysql_fetch_assoc())
print_r($row);

You need add fetch in cycle.

Upvotes: 1

Related Questions