Page 1 of 1

Passing function returns between scripts

Posted: Thu Aug 05, 2010 10:38 am
by samuelmoneill
Hi guys,

I have created a function for counting the rows in a mysql table as follows:

Code: Select all

<?php
include 'connect.php';


function my_count($table)
{
	echo $table;
	$query = "SELECT COUNT(*) FROM $table";
	$result = mysql_query($query);
	while($row = mysql_fetch_assoc($result))
	{
		$count = $row['COUNT(*)'];
	}
	echo $count;
	return $count;	
}

echo my_count(sectors);
?>
That works fine. But what I want to do is use that function in a different php file as follows:

Code: Select all

<?php
include 'connect.php';
include 'count.php';

$count = my_count(sectors);
echo $count;
?>
But that doesnt work. Why?

I know that I dont need to use my_count() in the first file, it was just to test it

Re: Passing function returns between scripts

Posted: Thu Aug 05, 2010 10:45 am
by AbraCadaver
Because you connect to the db in the connect.php I'm assuming, and that variable is not available in the function unless you pass it in or declare it global in the function.