Page 1 of 1

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

Posted: Tue Aug 23, 2005 7:27 am
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"]);
	}

Posted: Tue Aug 23, 2005 7:31 am
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;

Posted: Tue Aug 23, 2005 7:35 am
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

Posted: Tue Aug 23, 2005 7:42 am
by feyd
this will explain "function scope issue" better: http://php.net/language.variables.scope

Posted: Tue Aug 23, 2005 10:32 pm
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

Posted: Tue Aug 23, 2005 10:37 pm
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.

Posted: Tue Aug 23, 2005 10:57 pm
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);

Posted: Tue Aug 23, 2005 11:17 pm
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);
?>

Posted: Tue Aug 23, 2005 11:41 pm
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