OOP + Functions Help

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
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

OOP + Functions Help

Post 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;
	}
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

ohh haha. I feel stupid. Thanks.
Post Reply