Echo All Results of SQL Query

Associate
Joined
1 Mar 2006
Posts
425
hi guys

ive been trying to work this out now for a couple hours

i am a beginner and i am just trying to learn

i am sending a query to the database

Code:
$query=mysql_query("SELECT message FROM messages WHERE uid_fk=1");

these are the 3 results of that query

2nvt6xw.jpg


now when i try to show them on the webpage i only get the first results using

Code:
<?php

include_once 'includes/db.php';

$query=mysql_query("SELECT message FROM messages WHERE uid_fk=1");
$data = mysql_fetch_array($query);
echo $data['message'];

the below code counts the row correctly and shows "3"

Code:
$query2 = mysql_query("SELECT message FROM messages WHERE uid_fk='1'");
$data2=mysql_num_rows($query2);
echo $data2

how do i get the query to show all results

Ip9one test
testing p3oto upload
test

?

thanks
 
You need to loop through the results.

PHP:
$query=mysql_query("SELECT message FROM messages WHERE uid_fk=1");
while ($row = mysql_fetch_array($query)) {
  echo  $data['mesage'].'<BR>';
}
 
argh it should have been $row not $data and message with 2 s's

Code:
$query=mysql_query("SELECT message FROM messages WHERE uid_fk=1");
while ($row = mysql_fetch_array($query)) {
echo $row['message'];
}
thanks
 
Back
Top Bottom