droidus Posted September 23, 2011 Share Posted September 23, 2011 i am having issues returning all sent messages. it will only return one for some reason. //If there are sent messages, display them if ($row = mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) or die(mysql_error()); //Open table and create headers echo "<table border=\"1\">\n"; echo " <tr>\n"; echo " <th>Recipient</th>\n"; echo " <th>Subject</th>\n"; echo " </tr>\n"; while(mysql_fetch_array($result)) { //Show messages $userIDTo = $row['userIDTo']; // Get the recipient's ID number $recipient = checkRecipient($userIDTo); // Get the sender's Username $messageID = $row['ID']; echo " <tr>\n"; echo " <td>{$recipient}</td>\n"; echo " <td><a href='messageDetails.php?messageID=$messageID' target='_blank'>{$row['subject']}</a></td>\n"; echo " <tr>\n"; } thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/ Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 Are there only 2 messages that would be returned by the query you're currently testing with? You're moving the pointer to the second record before echoing anything by doing this: if ($row = mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) or die(mysql_error()); // <--- REMOVE THIS LINE Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272175 Share on other sites More sharing options...
droidus Posted September 23, 2011 Author Share Posted September 23, 2011 hm, i took it out, and it didn't return anything. there should be 2 entries that are returned though. Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272178 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 This should do it. Now you need to assign the value to $row here, rather than where it was. while( $row = mysql_fetch_array($result)) // NOT while(mysql_fetch_array($result)) Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272184 Share on other sites More sharing options...
droidus Posted September 23, 2011 Author Share Posted September 23, 2011 nope already tried it. still returning one. attached, is what my database looks like. here is my code for the sent messages: <?php function checkRecipient ($userIDTo) { // Find out who received the message (username) $query = "SELECT `uname`, `ID` FROM members WHERE ID='$userIDTo'"; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) or die(mysql_error()); $recipientUsername = $row['uname']; } return $recipientUsername; } mysql_select_db($database_uploader, $uploader); // Get the user's ID $query = "SELECT `uname`, `ID` FROM members WHERE uname='$_SESSION[user]'"; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) or die(mysql_error()); $userID = $row['ID']; } mysql_select_db($database_uploader, $uploader); // Get's all of the user's sent messages $query = "SELECT `userIDTo`, `subject`, `ID` FROM memberMail WHERE userIDFrom='$userID' AND sent='1'"; $result = mysql_query($query) or die(mysql_error()); //Display number of unread messages $num_rows = mysql_num_rows($result); mysql_select_db($database_uploader, $uploader); echo "You have ($num_rows) sent message(s): <p>"; //If there are sent messages, display them if ($row = mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result) or die(mysql_error()); //Open table and create headers echo "<table border=\"1\">\n"; echo " <tr>\n"; echo " <th>Recipient</th>\n"; echo " <th>Subject</th>\n"; echo " </tr>\n"; while( $row = mysql_fetch_array($result)) { //Show messages $userIDTo = $row['userIDTo']; // Get the recipient's ID number $recipient = checkRecipient($userIDTo); // Get the sender's Username $messageID = $row['ID']; echo " <tr>\n"; echo " <td>{$recipient}</td>\n"; echo " <td><a href='messageDetails.php?messageID=$messageID' target='_blank'>{$row['subject']}</a></td>\n"; echo " <tr>\n"; } //Close table echo "</table>\n"; } else { echo "bad"; } ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272186 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 Is that the current code? Why did you put that call to mysql_fetch_array() back in that I had you remove? All it effectively does is make it so the first record retrieved won't be displayed. Also, there's no reason to use or die(mysql_error()) with mysql_fetch_array(). MySQL doesn't return an error if that fails anyhow; you'd get a php error. Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272190 Share on other sites More sharing options...
droidus Posted September 23, 2011 Author Share Posted September 23, 2011 ok, so i changed it to: if (mysql_num_rows($result) > 0) { still not getting the two results that i should be i removed this line, and now it doesn't return anything $row = mysql_fetch_array($result); Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272200 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 Post the current code revision. Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272204 Share on other sites More sharing options...
droidus Posted September 23, 2011 Author Share Posted September 23, 2011 //If there are sent messages, display them if (mysql_num_rows($result) > 0) { //Open table and create headers echo "<table border=\"1\">\n"; echo " <tr>\n"; echo " <th>Recipient</th>\n"; echo " <th>Subject</th>\n"; echo " </tr>\n"; while(mysql_fetch_array($result)) { //Show messages $userIDTo = $row['userIDTo']; // Get the recipient's ID number $recipient = checkRecipient($userIDTo); // Get the sender's Username $messageID = $row['ID']; echo " <tr>\n"; echo " <td>{$recipient}</td>\n"; echo " <td><a href='messageDetails.php?messageID=$messageID' target='_blank'>{$row['subject']}</a></td>\n"; echo " <tr>\n"; } //Close table echo "</table>\n"; } else { echo "bad"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272207 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 while( $row = mysql_fetch_array( etc. Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272210 Share on other sites More sharing options...
droidus Posted September 24, 2011 Author Share Posted September 24, 2011 got it! thanks Quote Link to comment https://forums.phpfreaks.com/topic/247741-returning-messages/#findComment-1272212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.