Page 1 of 1

query in function

Posted: Tue Mar 22, 2011 3:23 pm
by miramichiphoto
Hi folks. I wrote this query and loop and works fine, but the minute I try to call it as a function, I get an error "mysql_query(): supplied argument is not a valid MySQL-Link resource in" on the line below the second query.

Here is the code. Thanks in advance.

Code: Select all

function fees() {
	
$user = $_SESSION['display_name'];
		$get_number_refs = "SELECT * FROM `users` WHERE `display_name`=$user"; // get number of refs from user db
		$result = mysql_query($get_number_refs, $connection) or die(mysql_error());
		$refs=mysql_num_rows($result);
		$i=0;
		
		while ($i < $refs) //outer loop # times = # refs
		
{// get total fees for each ref.
		

			$row = mysql_fetch_array($result);
			$ref_name = $row['display_name']; //get refs name
			$find_fees = "SELECT * FROM `games` WHERE `ref1`='".$row[display_name]."' || `ref2`='".$row[display_name]."'"; // gets all refs fees from db
			$result2 = mysql_query($find_fees, $connection) or die(mysql_error());
			$num=mysql_num_rows($result2); //get number of games
			$x = 0;
			$row2 = mysql_fetch_array($result2);
			$total_fees = 0;
			
				while ($x < $num) { //loop through each fee and add them up
				$total_fees = $total_fees + $row2['fee'];
				$x++;
									}
									
	echo $ref_name . " reffed  " . $num . " games totaling $" . $total_fees . "<br />";
	echo "and owes $" . number_format(($total_fees * 0.08),2,'.','') . "<br />";
	$i++;
									
}
	}

Re: query in function

Posted: Tue Mar 22, 2011 3:29 pm
by AbraCadaver
Because there is no $connection var in the function. Either pass it in or don't use it as mysql_query() will use the last connection by default.

Re: query in function

Posted: Tue Mar 22, 2011 10:29 pm
by miramichiphoto
Thank you from a newb.

L