-4

How do i do so it show all messages insted of only one, and other problem is that when the user have read there message and this part of the code right below are not showing. The full code can you find at the bottom of the page.

THIS CODE PART ARE NOT SHOWING AFTER A USER HAVE READ THERE NEW MESSAGE, BUT I WILL NOT SHOW:

    $newpm = '<div id="notificationTitle">Message</div>
    <div id="notificationsBody" class="notifications">You have no new messages</div>'; 

THE FULL CODE:

    $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">Message</div>
        <div id="notificationsBody" class="notifications">You have no new messages</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">Message</div>
                          <div id="notificationsBody" notifications">
<b><a href="page.php?name=profile&id='. $row['from'] .'">'. $row['subject'] .'</a></b><br> '. $row['text'] .'
                           '</div>'; 
            }
        }
    }
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 24 '15 at 12:24
  • *"The full code can you find at the bottom of the page."* - did you start the session? `session_start();` isn't mentioned/shown. – Funk Forty Niner Aug 24 '15 at 12:30
  • 1
    you also seem to have a syntax error. Look at Stack's syntax highlighting. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Aug 24 '15 at 12:31
  • the problem here is that you have an extra quote in `.' '';` and error reporting would have told you that. Syntax errors are off-topic, as per Stack's standards. – Funk Forty Niner Aug 24 '15 at 12:41
  • you also (re)posted this http://stackoverflow.com/q/32158253/ with an upvoted answer http://stackoverflow.com/a/32158400/ - so what's different with this question? – Funk Forty Niner Aug 24 '15 at 12:42
  • Seems like we're blowing bubble here *Sam* - @JayBlanchard – Funk Forty Niner Aug 24 '15 at 12:49
  • *Yessir, we sure are Ralph.* @Fred-ii- – Jay Blanchard Aug 24 '15 at 12:52
  • I hope the OP will bring us all back a coffee and danish when they get back. *What say you Sam?* - @JayBlanchard – Funk Forty Niner Aug 24 '15 at 13:00
  • *I could eat, Ralph.* @Fred-ii- – Jay Blanchard Aug 24 '15 at 13:13
  • *Me too Sam* @JayBlanchard I'm famished! – Funk Forty Niner Aug 24 '15 at 13:45

2 Answers2

0

First run this:

SELECT * FROM `pm` WHERE `to` = '<your SESSION ID>'
ORDER BY `id` DESC

and see if that returns something.

You can also do:

var_dump(mysql_num_rows($newpm_sql));die;  

after running this mysql_query in first line and see if it returns something you expect.

And also, you can switch to PDO - you will not regret it, as you're now using old php functions.

pablosd
  • 352
  • 4
  • 16
0

I see the error I missed last time.

This statement

$newpm = '<div id="notificationTitle">Message</div>
<div id="notificationsBody" class="notifications">You have no new messages</div>'; 

is being run inside a while loop and therefore you should be concatenating these multiple iterations onto $newpm using .= and not just an =

    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">Message</div>
            newpm .= '<div id="notificationTitle">Message</div>      
                      <div id="notificationsBody" notifications">
                         <b>
                            <a href="page.php?name=profile&id='. $row['from'] .'">'. $row['subject'] .'</a>
                        </b>
                        <br> '. 
                        $row['text'] .'
                      '</div>'; 
        }
    }
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149