Jump to content

[SOLVED] Using my variable twice


jandante@telenet.be

Recommended Posts

I'm a bit confused here. What I'm trying to do is run through a loop of "accounts" to display them. Before I do that I want to check if the accounts are in a user-specified region. Strangest thing is that when there are no accounts found in the specified region I get the correct message: "There are results, but not in you're region!". When there are accounts found in the region the statement checkRegion($result_getAccountID)>0 is found true and it shows me the message "Do it (Ben Stiller, Starsky & Hutch)" BUT it nevers enters the while. Cause "bla" never appears on the screen and I just get a white screen. So it must be that the statement $row = mysql_fetch_assoc($result_getAccountID) is alway's false (there are no more row in $result_getAccountID.

 

So I figured that my variable was already used in my function and just before calling the function I made a new variable "$checkRegion = $result_getAccountID". But that didn't work either. Anyone has an idea?

 

$result_getAccountID= mysql_query($query_getAccountID) or die(mysql_error());
/* If there are accounts found that match the tag_id go on */
$num_results_AccountID= mysql_num_rows($result_getAccountID);
if($num_results_AccountID > 0)
{
	if(checkRegion($result_getAccountID) > 0)
	{
		echo "Do it (Ben Stiller, Starsky & Hutch)";
		while($row = mysql_fetch_assoc($result_getAccountID))
		{
			echo "Do it (Ben Stiller, Starsky & Hutch)";
			/* Get the data of the accounts where we just extracted the ID from */
			echo "Do it!";
			$accountID = $row["account_id"];
			$query_getAccount="SELECT name, address FROM accounts WHERE id = ".$accountID." AND postalcode=".$where;
			$result_getAccount= mysql_query($query_getAccount) or die(mysql_error());
			while($rowAccount = mysql_fetch_assoc($result_getAccount))
			{
				/* Output */
				echo $rowAccount["name"];
				echo "<br />";
				echo $rowAccount["address"];
				echo "<br /><br />";
			}
		}
	}
	else
	{
		echo "There are results, but not in you're region!";
	}
}
else
{
	echo "You're search for ".$what." didn't bring up any results.";
}
}
else
{
echo "The word you're looking for is unknown";
}
function checkRegion($regioncheck)
{
$returnValue = 0;
$where = $_POST['where'];
while($rowRegioncheck = mysql_fetch_assoc($regioncheck))
{
	$accountIDregioncheck = $row["account_id"];
	$query_getAccountregioncheck = "SELECT name FROM accounts WHERE postalcode=".$where;
	$result_getAccountregioncheck = mysql_query($query_getAccountregioncheck) or die(mysql_error());
	$num_result_getAccountregioncheck = mysql_num_rows($result_getAccountregioncheck);
	if($num_result_getAccountregioncheck > 0)
	{
		$returnValue++;
	}
}
return($returnValue);
}

Link to comment
https://forums.phpfreaks.com/topic/138870-solved-using-my-variable-twice/
Share on other sites

From PHP.net

Description

array mysql_fetch_assoc ( resource $result )

 

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

 

Could it be that in my function the internal data pointer is at the end of my array and it the next while php thinks my array is alraedy at his end?

 

That would explain why "$row = mysql_fetch_assoc($result_getAccountID)" is false in my code. But how do I put it back?

I've found it. Just before executing my while after the function has run I did this:

"$result_getAccountID = mysql_query($query_getAccountID) or die(mysql_error());"

My while was true again and went through the loop.

 

Thanks for reading

Archived

This topic is now archived and is closed to further replies.

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