Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    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!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    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)

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Nope

    Local Variable:
    $foo = "bar";

    Global Variable:
    global $foo = "bar";

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

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •