View Full Version : Messaging system, displaying the messages
keyboard
08-24-2011, 05:47 AM
Hi everyone,
I'm trying to display all the messages one after another.
<?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?
JShor
08-24-2011, 05:45 PM
Try changing $message to $query. It could be a conflict.
<?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.
keyboard
08-24-2011, 08:24 PM
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
$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>';
?>
JShor
08-24-2011, 08:55 PM
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:
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.
keyboard
08-25-2011, 12:52 AM
It's now displaying the messages fine??? Me thinks it was a problem with the sql database Thanks for all your help jshor.
keyboard
08-25-2011, 06:46 AM
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
$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?
JShor
08-25-2011, 03:37 PM
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:
$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>";
keyboard
08-25-2011, 08:17 PM
Then how do I display the message based on the id in the url?
JShor
08-25-2011, 08:52 PM
The "ID" is a query string. Use the _GET variable to retrieve a query string variable's value.
<?php
$id = mysql_escape_string($_GET['ID']);
$query = mysql_query("SELECT * FROM messages WHERE id='$id'") or
die(mysql_error());
keyboard
08-25-2011, 11:03 PM
I'm not sure if I'm doing this right but here it is
<?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
$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
JShor
08-26-2011, 01:15 AM
You need to escape the double quotes.
echo "<h3><i><a href=\"read_message2.php?ID=$row[message_id]\"> ".$row['message_title']."</a></i></h3>"; }
keyboard
08-26-2011, 02:47 AM
<?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'].""; }
?>
( ! ) 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
That one works fine now , thanks JShor. What about this one?
JShor
08-26-2011, 01:22 PM
1. You never terminated your line containing mysql_fetch_array() with a semicolon, indicating the statement is incomplete.
2. You put two double quotes in your echo statement. Actually, you don't need quotes at all since you're just echoing one variable.
Try this:
$row = mysql_fetch_array ($query );
echo $row['message_content']; }
JShor
08-26-2011, 01:24 PM
Also, you seem to be making a lot of syntax/logical errors. I recommend using a PHP IDE such as phpDesigner, which will check the syntax live for you. If you don't want to use payware, you could use eclipse. There is a plugin for eclipse for PHP syntax.
keyboard
08-26-2011, 08:38 PM
Thanks JShor. One more thing. I've made two accounts on my website and when I login with the other one, (username= bob) I can still view all the messages adrresed to the other user. Any help
JShor
08-26-2011, 09:04 PM
For that, you'll need to check whether the user that is viewing the message is either the sender or the receiver of the message.
So if bob didn't send or receive the message they are viewing, then it would return an error like "You can't view this message, bob!".
Here's a quick example based on your previous code (although I don't quite remember what field you named your sender (for explanation purposes, I named it from_user).
$id = mysql_escape_string($_GET['ID']);
$message = mysql_query("SELECT * FROM messages WHERE message_id='$id' AND (to_user = '$userfinal' OR from_user = '$userfinal')") or
die(mysql_error());
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.