views:

51

answers:

2

Here is my current code:

  $sql = "SELECT * FROM user_posts";
  $result = mysql_query($sql); 
  $row = mysql_fetch_array($result);
  while ($row = mysql_fetch_array($result)) 
  { 
  print $row['message'];
  }

My goal is to show all of the data in that SQL database through an array. But currently, it only shows the latest one and nothing else. How am I able to do this? Thanks!

A: 

You're only getting the one row because you overwrite the $row variable with the values from your results array.

$sql = "SELECT * FROM user_posts";
$result = mysql_query($sql); 
while ($info = mysql_fetch_array($result)){ 
    print $info['message'];
}

Change it to something like that.

BraedenP
It worked great! Thanks!
Rohan
+1  A: 

You should remove this line

$row = mysql_fetch_array($result);

Apart from that it should display every message

Greg
Works great! So that means I should not specify the variable before to make it work?
Rohan
What you're doing there is effectively removing the first row from the results. Each call to mysql_fetch_array moves on to the next row
Greg