Passing function returns between scripts

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
samuelmoneill
Forum Newbie
Posts: 2
Joined: Tue Mar 11, 2008 7:52 am

Passing function returns between scripts

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

Re: Passing function returns between scripts

Post 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.
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.
Post Reply