...

View Full Version : registration: call to member function on a non-object



Bobbo171
01-08-2007, 08:07 AM
I have a registration class with the following method to verify that the username doesn't already exist in the database


function checkUsername(){
$q = sprintf("SELECT COUNT(*) AS ucount FROM user WHERE username = '%s'", $this->myUser->getUsername());
$ucount = $db->result($db->query($q), "result", 0); //error line
if($ucount > 0){
$this->error = true;
$this->errors .= "-Username already exists\n";
}
}

the error I get is this

Fatal error: Call to a member function result() on a non-object in C:\Program Files\xampp\htdocs\clsRegister.php on line 44
and I cant figure out what is going wrong, the database functions that are being used are


public function query($query){
return mysql_query($query);
}
public function result($result, $type = 'assoc', $row = 0){
switch ($type){
case 'assoc':
return mysql_fetch_assoc($result);
break;
case 'row':
return mysql_fetch_row($result);
break;
case 'num_rows':
return mysql_num_rows($result);
break;
case 'result':
return mysql_result($result, $row);
break;
}
}

I've searched around for a while but couldn't find anything too useful to help me out. Any help is greatly appreciated:D Thanks!

firepages
01-08-2007, 01:57 PM
what does function checkUsername() know about $db ? , basically nothing unless you either globalise it


<?php
function checkUsername(){
global $db;//database object
//etc
}
?>

pass it as a parameter..

<?php
function checkUsername($db){
//etc
?>

or (preferably) have $db as an aggregated class variable...

<?php
class blah{
function blah(){
$this->db=new db();
}

function blah_blah(){
echo $this->db;
}
}
?>

Bobbo171
01-08-2007, 10:16 PM
thanks, I used the last method but just out of curiousity how would I go about globalising it? It's more than just putting global in front of it right?

whizard
01-09-2007, 06:20 AM
Nope

Local Variable:
$foo = "bar";

Global Variable:
global $foo = "bar";

More Info: http://us3.php.net/global

HTH
Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum