I’m trying to use $_SESSION[‘valid_user’] in a .php script that accesses the table “mail” under “users.” $_SESSION[‘valid_user’] has been defined in a script which I included. Whenever I use “WHERE to=$_SESSION[‘valid_user’]” in my SELECT statement, I get a blank page. However, if I take it out, the script runs and displays all messages in the database, not just the message that was defined to show to that particular username. Despite this, I can echo $_SESSION[‘valid_user’] outside of the while loop or SELECT statement. Here’s my code:
<?php
include("mainmenu.php");
include("checklogin.php");
//$_SESSION[‘valid_user’] defined in checklogin.php
$con = mysql_connect("localhost", "root", "g00dfor@boy");
if(!$con){
die(mysql_error());
}
mysql_select_db("users", $con);
$result = mysql_query("SELECT * FROM mail WHERE to=$_SESSION['valid_user']");
//when executed with WHERE to=$_SESSION[‘valid_user’] it displays blank page.
while($row = mysql_fetch_array($result))
{
echo "To: " . $row['to'] . "| From: " . $row['from'] . "<br/>";
echo "Subject: " . $row['subject'] . "<br/><br/>" . "Message: " . $row['message'];
echo "<br/>";
}
mysql_close($con);
?>
Don’t say, “Put $_SESSION[‘valid_user’] in double quotes.” I’ve already tried that.
Change to
$result = mysql_query("SELECT * FROM mail WHERE to='".$_SESSION['valid_user']."'");