Jump to content

For and While loops display blank page.


McBryver

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)."!";

}

}


}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...";
}
?>

Link to comment
Share on other sites

Ok great thank you PFMaBiSmAd it works and ranks my players :P

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.