MrXkill Posted July 24, 2012 Share Posted July 24, 2012 Hi! I am programming an alliance page for a game I am making and I'm having some trouble. 1. When the If ($aid == 0) statement is true, it prints out "Choose an Alliance," but doesn't list the top 5 in the database (the LIMIT 5 in the sql query). 2. When the else statement is true, the page is completely blank. Any ideas? Thanks in advance. Here's my code: (located at top of page before html) <?php include_once('php/connect.php'); session_start(); ?> (located in a div) <?php $uid = $_SESSION['uid']; $username = $_SESSION['username']; $sql = "SELECT * FROM hack_wars_users WHERE uid='".$uid."' LIMIT 1"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res) or die(mysql_error()); $aid = $row['aid']; if ($aid == 0) { $sql2 = "SELECT * FROM alliances WHERE aid >= '1' LIMIT 5"; $res2 = mysql_query($sql2) or die(mysql_error()); echo "<h1>Choose an Alliance</h1><br />"; while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) { $alliancename = $row2['name']; $alliancedes = $row2['description']; echo "Name: ".$alliancename." Description: ".$alliancedes."<br />"; } Echo "<hr /><br /><h2>Search for an Alliance</h2><br /> <from action='php/alliancesearch.php' method='post'>Search: <input type='text' value='Alliance Name' name='name'> <input type='submit' value='Search' name='search'></form><br />"; } else { $sql2 = "SELECT * FROM alliances WHERE aid='".$aid."'LIMIT 1"; $res2 = mysql_query($sql2) or die(mysql_error()); $row2 = mysql_fetch_assoc($res2) or die(mysql_error()); $alliancename = $row2['name']; $alliancenews = $row2['news']; $oid = $row['oid']; $sql3 = "SELECT * FROM hack_wars_users WHERE uid='".$oid."' LIMIT 1"; $res3 = mysql_query($sql3) or die(mysql_error()); $row3 = mysql_fetch_assoc($res3) or die(mysql_error()); $oname = $row3['username']; echo "<h2>".$alliancename."</h2><br /> <h3>Leader: ".$oname."</h3><br /> <p>News: ".$alliancenews."</p><br /><hr /><br /> <h2>Leave Alliance</h2><br /> <form action='php/leavealliance.php' method='post'><input type='submit' value='Leave Alliance' name='leave'></form>"; } ?> </div> PS: all session variables, etc. have been set on a login page. mod edit: add code tags Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/ Share on other sites More sharing options...
scootstah Posted July 24, 2012 Share Posted July 24, 2012 Please put code in tags. 1. So, what is the output? 2. Is there an error perhaps? Do you have error reporting on? Are you using Apache? Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1363941 Share on other sites More sharing options...
g__ink Posted July 24, 2012 Share Posted July 24, 2012 while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) { the double equals (==) and the or die(mysql_error() looks like your problem. changing it to the following should help. while ($row2 = mysql_fetch_assoc($res2)) { Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1363956 Share on other sites More sharing options...
MrXkill Posted July 24, 2012 Author Share Posted July 24, 2012 Please put code in tags. 1. So, what is the output? 2. Is there an error perhaps? Do you have error reporting on? Are you using Apache? Sorry, this was my first post. I'll put my code inside of code tags next time. 1. A. When the If ($aid == 0) statement is true, it prints out "Choose an Alliance," but doesn't list the top 5 in the database (the LIMIT 5 in the sql query). B. When the else statement is true, the page is completely blank. Any ideas? Thanks in advance. Here's my code: 2. No errors print on the display. I am using apache, and how do I turn error reporting on? I get sql errors and errors (such as a missing ';' in my script). ----------- while ($row2 == mysql_fetch_assoc($res2) or die(mysql_error())) { the double equals (==) and the or die(mysql_error() looks like your problem. changing it to the following should help. while ($row2 = mysql_fetch_assoc($res2)) { I tried the php code, and I got the same output to my browser. Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1364045 Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 If you run the query at command line or through phpMyAdmin what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1364048 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2012 Share Posted July 24, 2012 Did you remove the or die() after all the mysql_fetch_assoc statements? If there are no rows in a result set, the mysql_fetch_assoc statement returns a false value which causes the or die() statement to be executed. But because this isn't a mysql error, the mysql_error() output is empty and your page dies at that point with no output. Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1364049 Share on other sites More sharing options...
MrXkill Posted July 24, 2012 Author Share Posted July 24, 2012 If you run the query at command line or through phpMyAdmin what do you get? It says Showing rows 0 - 0 (5 total, Query took 0.0005 sec) and shows me the alliances. Did you remove the or die() after all the mysql_fetch_assoc statements? If there are no rows in a result set, the mysql_fetch_assoc statement returns a false value which causes the or die() statement to be executed. But because this isn't a mysql error, the mysql_error() output is empty and your page dies at that point with no output. It worked! Thanks, I have always used that in all of my php and I've never gotten any errors. Thank you for all of the help! Quote Link to comment https://forums.phpfreaks.com/topic/266154-php-while-loop-help-other-stuff/#findComment-1364055 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.