Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Messaging system, displaying the messages

  1. #1
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default Messaging system, displaying the messages

    Hi everyone,

    I'm trying to display all the messages one after another.

    PHP Code:
    <?php
    $userfinal
    $_COOKIE['ID_my_site'];  
    require 
    "database.php";


    $message mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or
    die(
    mysql_error());


    while (
    $row mysql_fetch_array ($message)) { 
    echo 
    "<h1>Title: ".$message['message_title']."</h1><br><br>";
    echo 
    "<h3>From: ".$message['from_user']."<br><br></h3>";
    echo 
    "<h3>Message: <br>".$message['message_contents']."<br></h3>";
    }

    echo 
    '<form name="backfrm" method="post" action="inbox.php">';
    echo 
    '<input type="submit" value="Back to Inbox">';
    echo 
    '</form>';



    ?>
    It's coming up with this error

    ( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\edited pm\read_message.php on line 19
    Call Stack
    # Time Memory Function Location
    1 0.0003 371352 {main}( ) ..\read_message.php:0
    2 0.0150 378784 mysql_fetch_array ( ) ..\read_message.php:19


    and I'm not sure if I'm even doing the code right. Any help?
    Last edited by keyboard; 08-25-2011 at 06:43 AM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Try changing $message to $query. It could be a conflict.

    PHP Code:
    <?php
    $userfinal
    $_COOKIE['ID_my_site'];  
    require 
    "database.php";


    $query mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or
    die(
    mysql_error());


    while (
    $row mysql_fetch_array ($query )) { 
    echo 
    "<h1>Title: ".$row['message_title']."</h1><br><br>";
    echo 
    "<h3>From: ".$row['from_user']."<br><br></h3>";
    echo 
    "<h3>Message: <br>".$row['message_contents']."<br></h3>";
    }

    echo 
    '<form name="backfrm" method="post" action="inbox.php">';
    echo 
    '<input type="submit" value="Back to Inbox">';
    echo 
    '</form>';



    ?>
    By the way, $row is returning the array of data, not $message. I changed that for you in the code.
    - Josh

  3. #3
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Thanks Jshor for your help. When I open the page all I see is the button to go back to the main page. The messages arn't dispaying. The table they are stored in is called messages. The error message is gone.

    I changed the code to what you suggested.

    PHP Code:
    <?php
    $userfinal
    $_COOKIE['ID_my_site'];  
    require 
    "database.php";


    $query mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or
    die(
    mysql_error());


    while (
    $row mysql_fetch_array ($query )) { 
    echo 
    "<h1>Title: ".$row['message_title']."</h1><br><br>";
    echo 
    "<h3>From: ".$row['from_user']."<br><br></h3>";
    echo 
    "<h3>Message: <br>".$row['message_contents']."<br></h3>";
    }

    echo 
    '<form name="backfrm" method="post" action="inbox.php">';
    echo 
    '<input type="submit" value="Back to Inbox">';
    echo 
    '</form>';



    ?>

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    That probably means that when executing SELECT * FROM messages WHERE to_user = '$userfinal', nothing is returned (zero rows match).

    Try adding this at the end of your script:
    PHP Code:
    echo "Number of rows matching query: <b>".mysql_num_rows($query)."</b>"
    If it returns a message of "Number of rows matching query: 0", then it means that nothing matches your query and no rows are being returned.
    - Josh

  5. #5
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    It's now displaying the messages fine??? Me thinks it was a problem with the sql database Thanks for all your help jshor.

  6. #6
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    I've got another question.

    I would like to have the title of the message act as a hyperlink to another page where it would display the message which has the same id as the title?

    So something like this

    PHP Code:
    $query mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or 
    die(
    mysql_error()); 


    while (
    $row mysql_fetch_array ($query )) {  

    echo 
    "<h3><i><a href=""> ".$row['message_title']."</a></i></h3>"
    But when i get to the page, how do i get the id of the title that was clicked on. Any help?

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Do you have a primary key/auto-increment column set? If so, that would be your ID.

    Assuming you have this column and you named it "id", the code would look like:
    PHP Code:
    $query mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or 
    die(
    mysql_error()); 


    while (
    $row mysql_fetch_array ($query )) {  

    echo 
    "<h3><i><a href="message.php?ID=$row[id]"> ".$row['message_title']."</a></i></h3>"
    - Josh

  8. #8
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Then how do I display the message based on the id in the url?

  9. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    The "ID" is a query string. Use the _GET variable to retrieve a query string variable's value.

    PHP Code:
    <?php

    $id 
    mysql_escape_string($_GET['ID']);

    $query mysql_query("SELECT * FROM messages WHERE id='$id'") or 
    die(
    mysql_error());
    - Josh

  10. #10
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,521
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    I'm not sure if I'm doing this right but here it is

    PHP Code:
    <?php 
    $userfinal
    $_COOKIE['ID_my_site'];   
    require 
    "database.php"


    $query mysql_query("SELECT * FROM messages WHERE to_user = '$userfinal'") or 
    die(
    mysql_error()); 


    while (
    $row mysql_fetch_array ($query )) {  

    echo 
    "<h3><i><a href="read_message2.php?ID=$row[message_id]"> ".$row['message_title']."</a></i></h3>"; } 
     
    <
    br /><br /><br /><br />
    <?
    php


    echo 
    '<form name="backfrm" method="post" action="inbox.php">'
    echo 
    '<input type="submit" value="Back to Inbox">'
    echo 
    '</form>'



    ?>
    Thats read_messagetitle.php and the error on it says


    ( ! ) Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\edited pm\New Folder\read_messagetitle.php on line 12


    PHP Code:
    <?php

    $id 
    mysql_escape_string($_GET['ID']);

    $query mysql_query("SELECT * FROM messages WHERE message_id='$id'") or 
    die(
    mysql_error());

    $row mysql_fetch_array ($query )

    echo 
    ".$row['message_content'].""; } 

    ?>
    Thats read_message2.php and it's coming up with this error

    ( ! ) Parse error: syntax error, unexpected T_ECHO in C:\Documents and Settings\Owner\Desktop\canberra amatuer productions\www\Canberra Amatuer Productions\edited pm\New Folder\read_message2.php on line 10


    Thanks for all your help,

    keyboard1333

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •