1

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?

5
  • inside your table has how many "value1"? Commented Apr 16, 2014 at 7:52
  • Show us your complete code. Commented Apr 16, 2014 at 7:53
  • 1
    value1 is in Column_2. And your where clause is on Column_1. Your query doesn't seem coherent with the sample data given. Commented Apr 16, 2014 at 7:54
  • 2
    Your source code does not do anything with the database. You have to actually send the query to the database and iterate over the result set. Commented Apr 16, 2014 at 7:55
  • 2 value1... sorry the ckause is in Column_2... Commented Apr 16, 2014 at 8:25

3 Answers 3

2
$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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks hoangvu68, your solution is detailed and perfect
1
$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.

1 Comment

Thanks Alexey, your answer hit the nail... except that you missed out $res in while($row = mysql_fetch_assoc($res)) and it's WHERE Column_2 = 'value1' "; but thaks anyway...
0
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'];
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.