I'm trying to make a social network and are adding a notification bar on the site, but the code is not loading the data from the database.
$newpm_sql = mysql_query("SELECT * FROM `pm`
WHERE `to` = '". $_SESSION['id'] ."'
ORDER BY `id` DESC") or die(mysql_error());
if (mysql_num_rows($newpm_sql) == 0) {
$newpm = '<div id="notificationTitle">Meddelande</div>
<div id="notificationsBody" class="notifications">
Du har inga meddelanden!
</div>
';
} else {
while ( $newpm = mysql_fetch_array( $newpm_sql )) {
$from_sql = mysql_query("SELECT * FROM `members`
WHERE `id` = '". $newpm['from'] ."'")
or die(mysql_error());
$from = mysql_fetch_array($from_sql);
if ($newpm['status'] == 0) {
$newpm = '<div id="notificationTitle">Meddelande</div>
<div id="notificationsBody" notifications">'.
$newpm['subject'] .' '. $newpm['from'] .
'</div>';
}
}
}
You are using the variable $newpm
for almost everything and therefore destroying what was held in it up to that point.
Use a new variable in the second query, I have used $row
as an example below
$newpm_sql = mysql_query("SELECT * FROM `pm`
WHERE `to` = '". $_SESSION['id'] ."'
ORDER BY `id` DESC") or die(mysql_error());
if (mysql_num_rows($newpm_sql) == 0) {
$newpm = '<div id="notificationTitle">Meddelande</div>
<div id="notificationsBody" class="notifications">Du har inga</div>';
} else {
while ( $row = mysql_fetch_array( $newpm_sql )) {
$from_sql = mysql_query("SELECT * FROM `members`
WHERE `id` = '". $newpm_sql['from'] ."'")
or die(mysql_error());
$from = mysql_fetch_array($from_sql);
if ($row['status'] == 0) {
$newpm = '<div id="notificationTitle">Meddelande</div>
<div id="notificationsBody" notifications">'.
$row['subject'] .' '. $row['from'] .
'</div>';
}
}
}
Also you should not be using the
mysql_
database access extension, it is deprecated i.e. soon to be removed. As you are obviously learning, spend your time learning themysqli_
extension or thePDO
extension. See this for some help deciding which you prefer Why shouldn't I use mysql_* functions in PHP?