Question

So here is what I have... the issue is it is giving me errors and I cant get it to work for the life of me!

<?php 
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database); 
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
while($row = mysql_fetch_assoc($result))
{
if($row['recieved'] == '1')
{
echo '<a class="new-message" title="New Message">New</a>';
}
else
{
echo '&nbsp;';
}
}
?>

$user is the SESSION Username (comes from a different code page)

What I want is for it to just show the New Message when it has not been read yet (=0) and show a space (or nothing) if the message has been read (=1).

Was it helpful?

Solution

Try this code

<?php 
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database); 
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
$row = mysql_fetch_row($result);
echo '<a class="new-message" title="New Message">New</a>';
?>

Your code is improper way because any time you want only one user data than why use while loop?

OTHER TIPS

   $result = mysql_query("SELECT * FROM messages WHERE reciever=$user");
  while($row = mysql_fetch_assoc($result))
    {
   if($row['recieved'] == 1)
    {
          echo '<a class="new-message" title="New Message">New</a>';
    }
    else
    {
         echo '&nbsp;';
    }
    }

try to use this and see if it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top