Page 1 of 1

Displaying multiple rows of data from the database

Posted: Tue Feb 08, 2011 3:08 pm
by taraxgoesxboom
I'm trying to display messages sent to users I have saved on the database, this is my code:

Code: Select all

    <?php
	  
	  $username = $_SESSION['username']; 
	  
	  $sql = mysql_query("SELECT * FROM mail WHERE to_user = '$username'") or die (mysql_error());
	 
	 while($row = mysql_fetch_array($sql)){

		echo $row['to_user'];
		echo $row['from_user'];
		echo $row['subject'];
		echo $row['message'];

}
	?>
It seems right to me, but for some reason I am not getting any results. Any ideas as to what I'm doing wrong here?
(Also, I do have a proper connection to the database and my session is working correctly)

Re: Displaying multiple rows of data from the database

Posted: Tue Feb 08, 2011 3:19 pm
by Jade
Try running your query in mysql to make sure it works there. If it does then check to make sure you have a value in your $_SESSION. If you do then try echo-ing the query you're building in PHP and copy/paste that into mysql to make sure it also works.

<?php

$username = $_SESSION['username'];

$sql = "SELECT * FROM mail WHERE to_user = '$username'";
echo $sql;
$query= mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_array($query)){

echo $row['to_user'];
echo $row['from_user'];
echo $row['subject'];
echo $row['message'];

}
?>