Page 1 of 1

OOP + Functions Help

Posted: Thu Mar 15, 2007 3:05 pm
by anthony88guy
Hello,

I've created a few functions that use some class functions as well. But I cant get the two to work together. I don't understand whats going on here. I've included as much information as I thought necessary.
Fatal error: Call to a member function on a non-object in login.function.php on line 4

Code: Select all

<?php
require("include/config.php");
require("include/mysql.class.php");
require("include/login.function.php");

$db = new mysql($db['host'], $db['username'], $db['password'], $db['database']);

checkUsername("tehbrosta");

$db->close();
?>

Code: Select all

//login.function.php
function checkUsername($username)
{
	$data = $db->query("SELECT username FROM `" . $tb['login'] . "` WHERE username = '" . $db->prepare($username) . "' LIMIT 1") or $db->raise_error(); //LINE 4
	
	if( $db->num_rows($data) == 1 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

Posted: Thu Mar 15, 2007 3:12 pm
by shiznatix
you have to pass the object $db to the function because the variable $db is not global.

do this:

Code: Select all

checkUsername($username, $db)
for both when you call the function checkUsername and for when you define the function. That should solve your problem.

Posted: Thu Mar 15, 2007 3:22 pm
by anthony88guy
ohh haha. I feel stupid. Thanks.