0

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>'; 
        }
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • What happens when you run this code? Does anything show up? Errors? White screen? Is there actually data that should be showing up? Have you checked that the SQL is generated properly? – andrewsi Aug 22 '15 at 16:23
  • You're using an API from the last millennium (well, nearly). Come join the 21st century. – Strawberry Aug 22 '15 at 16:28
  • use an error message at else part ,and use an echo comand to display $newpm_sql ,,tell us what is the result –  Aug 22 '15 at 16:29
  • At least here should be some `echo` – u_mulder Aug 22 '15 at 16:30

1 Answers1

1

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 the mysqli_ extension or the PDO extension. See this for some help deciding which you prefer Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Ok, thank you, how do i do so it show the 5 latest insted of only one. My new problem is that when the user have read there message and this should show instead. but are not. if (mysql_num_rows($newpm_sql) == 0) { $newpm = '
    Message
    You got no message
    ';
    – Per Johansson Aug 23 '15 at 17:01
  • Sorry that sounds like a new question. Ask another question – RiggsFolly Aug 23 '15 at 18:43
  • @RiggsFolly OP reposted Smokey http://stackoverflow.com/q/32182247/ - syntax error. – Funk Forty Niner Aug 24 '15 at 12:44
  • 1
    @Fred-ii- Got it and posted an answer fixing the error I missed last time – RiggsFolly Aug 24 '15 at 13:37