Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2007, 07:07 AM   PM User | #1
Bobbo171
Regular Coder

 
Join Date: Aug 2004
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Bobbo171 is an unknown quantity at this point
registration: call to member function on a non-object

I have a registration class with the following method to verify that the username doesn't already exist in the database
Code:
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
Quote:
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
Code:
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 Thanks!
Bobbo171 is offline   Reply With Quote
Old 01-08-2007, 12:57 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
what does function checkUsername() know about $db ? , basically nothing unless you either globalise it
PHP Code:
<?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;
  }
}
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 01-08-2007, 09:16 PM   PM User | #3
Bobbo171
Regular Coder

 
Join Date: Aug 2004
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Bobbo171 is an unknown quantity at this point
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?
Bobbo171 is offline   Reply With Quote
Old 01-09-2007, 05:20 AM   PM User | #4
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
Nope

Local Variable:
$foo = "bar";

Global Variable:
global $foo = "bar";

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

HTH
Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:59 PM.


Advertisement
Log in to turn off these ads.