McBryver Posted March 4, 2011 Share Posted March 4, 2011 Ok when I run this code is displays nothing but a white page.. No error or anything. I have commented the first two segments of code the rest should be self explainable. Ok my beauty of a script: Any help to improve and or modify would be great as well as helping with the problem at hand. <?php include("secure/database.php"); // Include Database Connection File... $q = "SELECT * FROM `accountinfo_org` ORDER BY `strikeaction` ASC";//Select all from account table order by strike action ascending . $res = mysql_query($q) or die(mysql_error()); // Query $count = mysql_num_rows($res); // Count for($i=1;$i>=$count;$i++){// For loop while($player=mysql_fetch_array($res)){ // While Loop $id = securevar($player['id']); // ID $user = securevar($player['username']); // Username $q = "UPDATE `accountinfo_org` SET `strikerank` = '$i' WHERE `id` = '$id'"; // Update account set strike action equals $i where account is account. $res = mysql_query($q) or die(mysql_error()); // Get Query if(isset($res)){ // if isset result from query. echo "Strike Rank set to $user as ".number_format($i)."!"; // Echo This. } } } $q = "SELECT * FROM `accountinfo_org` ORDER BY `defenceaction` ASC"; // Select all from table order by defenceaction ascending. $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `defencerank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ echo "Defence Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_org` ORDER BY `covertaction` AND `anticovertaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `covertrank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ echo "Covert & Anti Covert Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_org` ORDER BY `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `msrank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ echo "Mothership Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_org` ORDER BY `msaction` AND `strikeaction` AND `defenceaction` AND `covertaction` AND `anticovertaction` AND `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $time = time(); $q = "UPDATE `accountinfo_db` SET `rank` = '$i', `lastTurnTime` = '$time' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ echo "Overall Rank set to $user as ".number_format($i)."!"; } } } ?> Brian Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/ Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 I don't follow your logic of executing a query, then executing the same query within a for loop based on the number of records the query found to begin with. Maybe if you explain what you're trying to accomplish it would help. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183045 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ok I died out $res and get: Resource id #5 I am currently hitting google hard. Still no close to figuring this out. Btw Here is my Database Connection file: <?php session_start(); // Variable Overview $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'carpodiem'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); $dbname = 'database_info'; mysql_select_db($dbname); include("secure/mainfuncs.php"); ?> Thank you. Brian Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183066 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ok what I am trying to do is set the rank of each user by order of how it is grabbed by the query if users are grabbed like this: user 2 Set Rank to 1 user 3 Set Rank to 2 user 1 Set Rank to 3 hopefully this helps Pikachu2000 brian Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183070 Share on other sites More sharing options...
Pikachu2000 Posted March 5, 2011 Share Posted March 5, 2011 One error I see is the last query's ORDER BY clause should be separated by commas, not ANDs. If that doesn't fix anything, I'll need to dig into the code a little deeper. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183074 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ok mr pikachu I changed the AND to commas. Still no change from white page. Here is the new code : <?php include("secure/database.php"); // Include Database Connection File... $q = "SELECT * FROM `accountinfo_db` ORDER BY `strikeaction` ASC";//Select all from account table order by strike action ascending . $res = mysql_query($q) or die(mysql_error()); // Query $count = mysql_num_rows($res); // Count for($i=1;$i>=$count;$i++){// For loop while($player=mysql_fetch_array($res)){ // While Loop mysql_free_result($res); $id = securevar($player['id']); // ID $user = securevar($player['username']); // Username $q = "UPDATE `accountinfo_db` SET `strikerank` = '$i' WHERE `id` = '$id'"; // Update account set strike action equals $i where account is account. $res = mysql_query($q) or die(mysql_error()); // Get Query if(isset($res)){ // if isset result from query. mysql_free_result($res); echo "Strike Rank set to $user as ".number_format($i)."!"; // Echo This. } } } $q = "SELECT * FROM `accountinfo_db` ORDER BY `defenceaction` ASC"; // Select all from table order by defenceaction ascending. $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ mysql_free_result($res); $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `defencerank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ mysql_free_result($res); echo "Defence Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_db` ORDER BY `covertaction`, `anticovertaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ mysql_free_result($res); $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `covertrank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ mysql_free_result($res); echo "Covert & Anti Covert Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_db` ORDER BY `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ mysql_free_result($res); $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `msrank` = '$i' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ mysql_free_result($res); echo "Mothership Rank set to $user as ".number_format($i)."!"; } } } $q = "SELECT * FROM `accountinfo_db` ORDER BY `msaction`, `strikeaction`, `defenceaction`, `covertaction`, `anticovertaction`, `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $count = mysql_num_rows($res); for($i=1;$i>=$count;$i++){ while($player=mysql_fetch_array($res)){ mysql_free_result($res); $id = securevar($player['id']); $user = securevar($player['username']); $time = time(); $q = "UPDATE `accountinfo_db` SET `rank` = '$i', `lastTurnTime` = '$time' WHERE `id` = '$id'"; $res = mysql_query($q) or die(mysql_error()); if(isset($res)){ mysql_free_result($res); echo "Overall Rank set to $user as ".number_format($i)."!"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183085 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ive never seen anything like this again thanks for any help whether or not It does any good. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183087 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 I have been reading posts on other forums that have to do with the Resource id #5 and have found in a few cases they used: mysql_fetch_assoc() how can I use this in my case if possible? Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183094 Share on other sites More sharing options...
DavidAM Posted March 5, 2011 Share Posted March 5, 2011 Take a look at the first part of your code ... I did some indenting so we can see it more clearly $q = "SELECT * FROM `accountinfo_org` ORDER BY `strikeaction` ASC";//Select all from account table order by strike action ascending . $res = mysql_query($q) or die(mysql_error()); // Query $count = mysql_num_rows($res); // Count for($i=1;$i>=$count;$i++){// For loop while($player=mysql_fetch_array($res)){ // While Loop $id = securevar($player['id']); // ID $user = securevar($player['username']); // Username $q = "UPDATE `accountinfo_org` SET `strikerank` = '$i' WHERE `id` = '$id'"; // Update account set strike action equals $i where account is account. $res = mysql_query($q) or die(mysql_error()); // Get Query if(isset($res)){ // if isset result from query. echo "Strike Rank set to $user as ".number_format($i)."!"; // Echo This. } } } on the second line here, you execute a query and store the RESOURCE in $res. On line 6 (above) you start a while loop retrieving data from that RESOURCE ($res). Then, about 4 lines later, you execute a different query assigning ITS RESOURCE to $res. When the while loop repeats and tries to retrieve data from $res, it is a DIFFERENT RESOURCE and the fetch either fails or returns a row with columns that do not match what you expect in the loop. When you execute another query inside of a loop, you MUST use a DIFFERENT variable for the resource that is returned. NOTE: I did not look at the rest of the code. You need to fix this, and review the rest of your code for similar situations. Good Luck, post back if you have more trouble. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183096 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2011 Share Posted March 5, 2011 In addition to everything else that has been pointed out that is makes no sense in the code, the logic test in the for() statement is backwards AND since the for() loop is attempting to iterate over the result set from the first query AND that is what the while() loop does, there's no point in having that for() loop in the code at all. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183097 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ok as for the for loop it indicates the times the loop runs so that it can add a rank according to the time the loop has been ran. I have changed the outer $res from the $res in the loop: $q = "SELECT * FROM `accountinfo_db` ORDER BY `strikeaction` ASC";//Select all from account table order by strike action ascending . $res2 = mysql_query($q) or die(mysql_error()); // Query $count = mysql_num_rows($res2); // Count while($player=mysql_fetch_row($res2)){ // While Loop for($i=1;$i>=$count;$i++){// For loop mysql_free_result($res2); $id = securevar($player['id']); // ID $user = securevar($player['username']); // Username $q = "UPDATE `accountinfo_db` SET `strikerank` = '$i' WHERE `id` = '$id'"; // Update account set strike action equals $i where account is account. $res = mysql_query($q) or die(mysql_error()); // Get Query // if isset result from query. mysql_free_result($res); echo "Strike Rank set to $user as ".number_format($i)."!"; // Echo This. } } But nothing has changed still white page. Brian Thanks thus far.... Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183098 Share on other sites More sharing options...
sayjoy Posted March 5, 2011 Share Posted March 5, 2011 try echoing something at the begining of each while loop as a test. It may be that your condition didnt execute true and this implies that there will be no running of the codes within the frame. but try something like: while (condition){ echo "something "; // continue your codes; } in this case, something is echoed for as many times as the condition executes true, else nothing is echoed. If nothing is echoed, you can then check your conditions to verify it executes true. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183100 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2011 Share Posted March 5, 2011 Edit: You will need to add an $i variable that gets incremented to the following code. Edit2: I have updated the posted code to include the appropriate counters. If we assume that the logic of what you are doing makes sense in your application, the following code executes the queries you have in your logic - <?php include("secure/database.php"); // Include Database Connection File... $q = "SELECT * FROM `accountinfo_db` ORDER BY `strikeaction` ASC";//Select all from account table order by strike action ascending . $res = mysql_query($q) or die(mysql_error()); if(mysql_num_rows($res)){ // players exist in the table $i = 1; // the rank counter while($player=mysql_fetch_array($res)){ // While Loop mysql_free_result($res); $id = securevar($player['id']); // ID $user = securevar($player['username']); // Username $q = "UPDATE `accountinfo_db` SET `strikerank` = '$i' WHERE `id` = '$id'"; // Update account set strike action equals $i where account is account. if(mysql_query($q)){ // query execuited without error and probably updated the row (unless the value was the same before/after...) echo "Strike Rank set to $user as ".number_format($i)."!"; // Echo This. } else { // query failed with an error // put your error reporting/logging code here... } $i++; } $q = "SELECT * FROM `accountinfo_db` ORDER BY `defenceaction` ASC"; // Select all from table order by defenceaction ascending. $res = mysql_query($q) or die(mysql_error()); $i = 1; while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `defencerank` = '$i' WHERE `id` = '$id'"; if(mysql_query($q)){ echo "Defence Rank set to $user as ".number_format($i)."!"; } else { // query failed with an error // put your error reporting/logging code here... } $i++; } $q = "SELECT * FROM `accountinfo_db` ORDER BY `covertaction`, `anticovertaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $i = 1; while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `covertrank` = '$i' WHERE `id` = '$id'"; if(mysql_query($q)){ echo "Covert & Anti Covert Rank set to $user as ".number_format($i)."!"; } else { // query failed with an error // put your error reporting/logging code here... } $i++; } $q = "SELECT * FROM `accountinfo_db` ORDER BY `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $i = 1; while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $q = "UPDATE `accountinfo_db` SET `msrank` = '$i' WHERE `id` = '$id'"; if(mysql_query($q)){ echo "Mothership Rank set to $user as ".number_format($i)."!"; } else { // query failed with an error // put your error reporting/logging code here... } $i++; } $q = "SELECT * FROM `accountinfo_db` ORDER BY `msaction`, `strikeaction`, `defenceaction`, `covertaction`, `anticovertaction`, `msaction` ASC"; $res = mysql_query($q) or die(mysql_error()); $i = 1; while($player=mysql_fetch_array($res)){ $id = securevar($player['id']); $user = securevar($player['username']); $time = time(); $q = "UPDATE `accountinfo_db` SET `rank` = '$i', `lastTurnTime` = '$time' WHERE `id` = '$id'"; if(mysql_query($q)){ echo "Overall Rank set to $user as ".number_format($i)."!"; } else { // query failed with an error // put your error reporting/logging code here... } $i++; } } else { // there are no rows/players in the table echo "No players exist, no data to process..."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183104 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Ok great thank you PFMaBiSmAd it works and ranks my players Altho I get this error: Strike Rank set to Photonic as 1! Warning: mysql_fetch_array(): 5 is not a valid MySQL result resource in C:\xampp\htdocs\GMAE\getNextTurn.php on line 10 Defence Rank set to Photonic as 1! Defence Rank set to Joe as 2! Covert & Anti Covert Rank set to Photonic as 1! Covert & Anti Covert Rank set to Joe as 2! Mothership Rank set to Photonic as 1! Mothership Rank set to Joe as 2! Overall Rank set to Photonic as 1! Overall Rank set to Joe as 2! Also I added a <br /> to the end of each echo. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183123 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Should I use mysql_fetch_row(); ? Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183124 Share on other sites More sharing options...
PFMaBiSmAd Posted March 5, 2011 Share Posted March 5, 2011 Remove the left over - mysql_free_result($res); statement that is at about line 10 in the code. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183131 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 Thank you very much topic solved and I am confident that I can modify this with no problem. Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183140 Share on other sites More sharing options...
McBryver Posted March 5, 2011 Author Share Posted March 5, 2011 I cannot find the Mark Solved button so Mods delete this! Quote Link to comment https://forums.phpfreaks.com/topic/229621-for-and-while-loops-display-blank-page/#findComment-1183142 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.