Value not being returned from 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
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Value not being returned from function

Post by Crashin »

Hi all. I'm trying to return a value from a function to the page that called it, but the value is not showing up. Please take a look at the following code and tell me where I'm going wrong.

This first snippet is the function call:

Code: Select all

<?php
// call the function to query the product/user tables
get_account($_POST['regNumber'], $_POST['mailZipCode']);

// if there is no match
if ($num == 0) {
	$error = "<span class="body"><strong><font color="#FF0000">Invalid registration number/zip code combination. Please re-enter your information.</font></strong></span> <br><strong>\$num</strong> = $num<br><br>";
}

// if there is only one match
if ($num == 1) {?>
	<script type="text/javascript" language="JavaScript">
		<!--
		location.href="display_account.php";
		//-->
	</script><?php
	exit;
}
?>
This snippet is the function, included in the page containing the previous snippet:

Code: Select all

<?php
// function to get account information based upon
// the reg number and zip code provided by the
// customer
function get_account($regNumber, $mailZipCode) {
	$query = "SELECT p.productID FROM 
		customers AS c, 
		products AS p 
	WHERE 
		p.regNumber = $regNumber AND 
		c.mailZipCode = $mailZipCode AND 
		p.customerID = c.customerID";
	$result = mysql_query($query) or die ('There''s a problem!' . mysql_error());
	$num = mysql_num_rows($result);
	return $num;
	/*echo "\$query = " . $query . " \$num = " . $num;
	exit;*/
}
?>
Obviously, I'm trying to return the $num variable. Thanks!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

do you not need single quotes inside your SQL statement around $regNumber' and $mailZipCode?

Code: Select all

<?php 
// function to get account information based upon 
// the reg number and zip code provided by the 
// customer 
function get_account($regNumber, $mailZipCode) { 
   $query = "SELECT p.productID FROM 
      customers AS c, 
      products AS p 
   WHERE 
      p.regNumber = '$regNumber' AND 
      c.mailZipCode = '$mailZipCode' AND 
      p.customerID = c.customerID"; 
   $result = mysql_query($query) or die ('There''s a problem!' . mysql_error()); 
   $num = mysql_num_rows($result); 
   return $num; 
   /*echo "\$query = " . $query . " \$num = " . $num; 
   exit;*/ 
} 
?>
Mark
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

No, they're numeric values. :wink:
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

what does an echo $num (placed in the function-call snippet) give as value?
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Nothing (i.e. empty).
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

and what's it's value in the second snippet (before you return $num)?
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Well, it depends on the number of results returned (obviously). But, using a known sample it returns 1.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

gah, silly me. Bloody brain is fried...
So, why don't you just assign the value that function get_account() returns to a variable?

Would look like this

Code: Select all

$num=get_account($_POST['regNumber'], $_POST['mailZipCode']);
;)
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

As it turns out, this works:

Code: Select all

<?php
$num = mysql_num_rows(get_account($_POST['regNumber'], $_POST['mailZipCode']));
?>
If I only do this in the function:

Code: Select all

<?php
function get_account($regNumber, $mailZipCode) {
	$query = "SELECT * FROM 
		customers AS c, 
		products AS p 
	WHERE 
		p.regNumber = $regNumber AND 
		c.mailZipCode = $mailZipCode AND 
		p.customerID = c.customerID";
	$result = mysql_query($query) or die ('There''s a problem!' . mysql_error());
	return $result;
}
?>
This actually affords me a lot more flexibility with re-using the query. I'm not restricted to only getting the number of rows returned, but can also get the data in the rows. 8)

Thanks for the input!! :D
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

btw.: if you want to retain the return-value of a function, you have to assign it to a variable in the function-call.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Can you elaborate?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

It's what we've just done, e.g.:

Code: Select all

$num=get_account(); 
echo $num;
function get_account()
{
return "Hello";
}
See also http://uk.php.net/manual/en/functions.r ... values.php
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Ohhhhhhhhhhh...I thought you meant that it had to be a parameter in the function. I see...thanks!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

lazy

Post by phpScott »

Of course you could be lazy and just do:

Code: Select all

<?php
function some fund()
{

global $someValue;

$someValue="here is a global variable";

}


echo "someValue is ".$someValue;

?>
would also get you the value.

phpScott
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Yes, but then it's there FOREVER...which you might want anyway...
Post Reply