query in function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
miramichiphoto
Forum Newbie
Posts: 14
Joined: Thu Mar 17, 2011 1:12 pm

query in function

Post 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++;
									
}
	}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: query in function

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
miramichiphoto
Forum Newbie
Posts: 14
Joined: Thu Mar 17, 2011 1:12 pm

Re: query in function

Post by miramichiphoto »

Thank you from a newb.

L
Post Reply