SapAuthor Posted August 10, 2008 Share Posted August 10, 2008 I can't figure this out. In my code, i have one while statement to gather data from a MySQL query, and then another while statement to get data from another. I get an error that the variable in the 2nd while statement is undefined. I switched the two while statements around, and then the first ran but all the 2nd while statement's variables didn't show. Here's the code below, anyone have any ideas? The problem is in the 2nd while statement located here: //Getting the corresponding prayer to show with the message $sql = "SELECT * FROM prayers WHERE id=$id"; $res = mysql_query($sql); while ($newArray = mysql_fetch_array($res)) { $prayermessage = $newArray['request']; } Here's the complete thing <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>SaP Prayer - Message Approvel System</title> </head> <body> <?PHP $mysql = mysql_connect("localhost", "-------", "---------"); mysql_select_db("-------", $mysql); //This will do the operation when a message's fate has been determined if (isset($_POST['approve'])) { } $sql = "SELECT * FROM prayers_comments WHERE approved = 'n'"; $res = mysql_query($sql); $messagesleft = mysql_num_rows($res); $sql3 = "SELECT * FROM prayers_comments WHERE approved = 'n' LIMIT 0,1"; $res3 = mysql_query($sql3); if ($messagesleft == 0) { echo "There are no more messages left to approve. God Bless.";} //Function to bring up next prayer. if ($messagesleft != 0) { echo "There are ".$messagesleft." messages left to approve.\n\n"; while ($newArray = mysql_fetch_array($res)) { $prayerid = $newArray['prayerid']; $id = $newArray['id']; $message = $newArray['message']; $visible = $newArray['visible']; $email = $newArray['email']; $name = $newArray['name']; } //Getting the corresponding prayer to show with the message $sql = "SELECT * FROM prayers WHERE id=$id"; $res = mysql_query($sql); while ($newArray = mysql_fetch_array($res)) { $prayermessage = $newArray['request']; } echo "The Prayer Message is as follows:<br /><br />"; echo $prayermessage."<br /><br /><br />"; echo "The message is as follows, sent by ".$name."<br /><br />"; echo $message."<br /><br />"; ?> <form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post" name="messageapprove"> <input type="hidden" name="messageid" value="<?PHP echo $id; ?>" /> Visible: <input name="visible" type="radio" value="y" <?PHP if ($visible == 'y') {echo 'checked="checked"';} ?>/> Public <input name="visible" type="radio" value="n" <?PHP if ($visible == 'n') {echo 'checked="checked"';} ?>/> Private Only <br /> Action: <input name="action" type="radio" value="a" checked="checked"/> Approve <input name="action" type="radio" value="d" /> Delete <input name="action" type="radio" value="b" /> Delete & Ban User<br /> <br /> <input type="submit" name="Submit" value="Submit" /> </form> <?PHP } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/ Share on other sites More sharing options...
deadlyp99 Posted August 10, 2008 Share Posted August 10, 2008 Without diving too far into the proccess of debugging, i'm going to teach you something you NEED to make a habit. mysql_query($sql) or die("Error #" . mysql_error()); Make a habit of having a number and using the "or die" with at least "mysql_error()". The "Error #" part is optional, but can help pinpoint which query is goofing up. If a query isn't going, its with good reason. Also I noticed that you are echoing outside of the while loop. Its a good idea to echo inside of it, because each round on the loop, every variable is getting assigned a new value. Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/#findComment-612719 Share on other sites More sharing options...
SapAuthor Posted August 10, 2008 Author Share Posted August 10, 2008 Thanks for the tip, i'll start doing that. Both queries do give a 1 table result (as i said, when i switched their spots, the other one ran). I tried echoing inside the while statement, that's when i found that the echo didn't run, meaning anything in the 2nd while statement isn't running at all. Is there some rule against doing two while statements inside the same if statement? Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/#findComment-612729 Share on other sites More sharing options...
deadlyp99 Posted August 10, 2008 Share Posted August 10, 2008 Not that know of, I have never encountered that error. instead of having that trailing " } " inside its own php tags, try echoing all that html and keep it all in the same php tag so there isn't any disruption in the if statement. I can remember if this works off hand, but try in the while loops to add die statements as well. while ($new = mysql_fetch_array($var) or die(mysql_error)){//stuff} I think that the second fetching of an array from the same table is more likely what is causing the problem, because two while's in an If should be just fine. Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/#findComment-612736 Share on other sites More sharing options...
SapAuthor Posted August 10, 2008 Author Share Posted August 10, 2008 Ah...i'm such a moron. $id should be $prayerid, i did an echo statement of what the id was showing...yeah.... -.- there was no Mysql error because it did return a result, just 0 rows... Annnnnd thank you for your time. This is why i should never code after midnight I'll start using the error statement, working on that habbit. Thanks, i learned something at least Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/#findComment-612765 Share on other sites More sharing options...
deadlyp99 Posted August 10, 2008 Share Posted August 10, 2008 Through my years of coding, the best way to learn is to make 100 mistakes and fix them one by one. Glad to help, don't forget the solved button at the bottom of the screen Link to comment https://forums.phpfreaks.com/topic/118989-dual-while-with-mysql-query-first-one-causes-2nd-to-not-run-help/#findComment-612798 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.