[solved] my first function, be gentle on me ;)

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
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

[solved] my first function, be gentle on me ;)

Post by robster »

Hi all, here it is, my first attempt at a function.

Could anyone tell me why this won't work?

Code: Select all

//Find the actual name of a town based on its ID from the database
	function findtown( $city_closest )
	{
   		mysql_select_db($dbname);
		$sql = "SELECT * FROM towns WHERE town = $city_closest";
		$content = mysql_query($sql);
		$Xcontent = mysql_fetch_array($content);	
	
			return $town_id = $Xcontent["id"];
			return $town_name = $Xcontent["town"];
			return $town_state = $Xcontent["state"];
			return $town_popularity = $Xcontent["popularity"];
	}
if I call this with, nothing happens (that I can see when echoing $town in a script):

Code: Select all

$town = findtown($city_closest);
(noting that $city_closest is an integer that matches an ID in the table)


I have connected to the database already in the script before I call the function and I've included the function in my script (as it's in a seperate file).

Stumped :(

Rob



UPDATE!!
I've tried this also, but no luck :(

Code: Select all

function findtown( $city_closest )
	{
   		mysql_select_db($dbname);
		$sql = "SELECT * FROM towns WHERE town = $city_closest";
		$content = mysql_query($sql);
		$Xcontent = mysql_fetch_array($content);	
	
			return ($town_id = $Xcontent["id"]);
			return ($town_name = $Xcontent["town"]);
			return ($town_state = $Xcontent["state"]);
			return ($town_popularity = $Xcontent["popularity"]);
	}
Last edited by robster on Tue Aug 23, 2005 11:41 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$dbname is null, function scope issue.

multiple return values are often done with an array, as php will not parse any more of the function after it hits the first return keyword.

Code: Select all

global $dbname;
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I'm such a neub, sorry about this.

I don't know what you mean by $dbname being a null, function scope issue (:().
I also don't understand the global $dbname; thing.

Are you saying that $dbname needs to be a global? I call it in the previous script from an included file. I'll try what you say though, I think it's coming to me ;)

Also, I will have a try and feeding those results into an array and just return the array.

Thanks so much. Much appreciated

Rob
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this will explain "function scope issue" better: http://php.net/language.variables.scope
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

in config.php (the file I include in all scripts that need access to the database) I have now changed from this:

Code: Select all

$dbname = "mydatabase"; 	// name of the database
to this

Code: Select all

$dbname = "mydatabase"; 	// name of the database
global $dbname;
I then go and try and call my function but it still doesn't work. And to check if $dbname can be seen or not in the function, I do an echo of $dbname and it shows nothing. It is still not being seen in the function.

I'm not sure what else to do now. I've also included config.php into the function script file at the start, but that also does not help.

Any help appreciated.

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

actually, it turns out, sticking the include "config.php"; line INTO the actual function did the trick, it now sees the name of the database. The function is still not working, but I'll work through it a bit more to see what that is before asking any more questions.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Nope, still REALLY stuck.

This is what I have now, considering that $dbname can now be seen by the function and the call to the database works (it works outside of the function so I presume it works inside it):

Code: Select all

function findtown( $city_closest )
	{
		include "config.php";
		
   		mysql_select_db($dbname);
		$sql = "SELECT * FROM towns WHERE id = $city_closest";
		$content = mysql_query($sql);
		$Xcontent = mysql_fetch_array($content);	
	
			$town_id = $Xcontent["id"];
			$town_name = $Xcontent["town"];
			$town_state = $Xcontent["state"];
			$town_popularity = $Xcontent["popularity"];
			
			global $town_name;
			
			return ($town_name);  //turn this into an array of all the results
	}
I am trying to call the function like so:

Code: Select all

$town = findtown($city_closest);
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

try this:

Code: Select all

<?
include "config.php";

function findtown( $city_closest )
{
  global $dbname;
  mysql_select_db($dbname);
  $sql = "SELECT * FROM towns WHERE id = '$city_closest'";  
  $content = mysql_query($sql);
  $Xcontent = mysql_fetch_array($content);	
	
  $town = array();
  $town['id'] = $Xcontent["id"];
  $town['name'] = $Xcontent["town"];
  $town['state'] = $Xcontent["state"];
  $town['popularity'] = $Xcontent["popularity"];
  return ($town);
}

$town_array = findtown($city_closest);
?>
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

That worked an absolute TREAT!

It all makes so much sense now also. Thank you so much, I'm off now to re-write all that mysql gaff that's messing up my scripts into nice neat functions.

Production speed, here I come!

Thanks again everyone :)


Rob
Post Reply