SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    call to member function error

    Hey there,

    I am trying to work with inserting data into a database, when i go to insert it i receive "Fatal error: Call to a member function prepare() on a non-object" so that means that the pdo variable is not working right? In this case i am using $pdo for the pdo initiation in the connect.php file

    (var's defined)
    PHP Code:
    try{
    $pdo = new PDO('mysql:host='.$host.';dbname='.$database$user,$pass);
    }
    catch(
    PDOExexption $e){
        echo 
    "<div class=\"dbError\">\n    <p>There was an error connecting to the database: ".$e->getMessage()."</p>\n</div>";

    and insert/check then insert query

    PHP Code:
    public function add()
            {    
                
    $query['1'] = 'SELECT COUNT(*) FROM users WHERE email = :email';
                
    $values['1'] = array('email' => $this->email);
                
    $check $pdo->prepare($query['1']);
                            
                if(
    $res $check->execute($vlaues['1']))
                    {
                        if(
    $check->fetchColumn() < 1)
                            {
                                
    $query['2'] = 'INSERT INTO users SET
                                        email = :email,
                                        password = :password,
                                        dateJoined = CURDATE(),
                                        farmName = :farmName,
                                        name = :name,
                                        salt1 = :salt1,
                                        salt2 = :salt2,
                                        limt_results = `15`,
                                        level = `2`,
                                        reciveEmail = :recive'
    ;
                                
    $values['2'] = array('email' => $this->email'password' => $this->password'farmname' => $this->farmname'name' => $this->farm'salt1' => $salt['2'], 'salt2' => $salt['1'], 'reciveEmail' => $this->reciveEmail );
                
                                
    $insert $pdo->prepare($query['2']);
                                
    $insert->execute($values['2']);


                                
    $message "Hello {$this->name}, \n  your account has been created! you can login with {$this->email} and your correct password";
                                
    mail($this->email,'no-reply@ageasy.ca','Your New Account!',$message,'support@ageasy.ca');

                                                            
                            } 
    do you see anything i am missing?

    Thanks!
    -Colin

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You need to declare your $pdo variable as a global within the function itself, see the below.

    PHP Code:
    public function add() {
        global 
    $pdo;
        
        
    // Code here...


  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Using the global keyword within an object...that is just bad voodoo...
    PHP Code:
    class WhateverYouCallThis
    {
      protected 
    $pdo;

      public function 
    __construct PDO $pdo ) {
        
    $this->pdo $pdo;
      }

      public function 
    add () {
        
    $this->pdo->prepare();
      }
    }

    $t = new WhateverYouCallThis( new PDO( ... ) ); 
    That is how you do it.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks!,

    What you are saying is to make a new class for PDO and then for inserting data use $member->pdo->add()??


    Just out of curiosity, why is it bad to use global within an object?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    // somewhere else, you instantiate your db connection
    // your WhateverYouCallThis object doesn't care what kind of 
    // PDO object it is given, just that it must be given one or it will not work

    $t = new WhateverYouCallThis( new PDO( ... ) ); 

    // you don't honestly care how your new object does it, you just tell it to 
    // add().  Arguably it is implicit that it is addNewRecordToDatabase() because 
    // you just gave it a PDO connection in its constructor

    $t->add(); 
    Why using globals is wrong, related to why you should prefer composition over inheritance which is the example logic_earth showed you.

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    Thanks for the article Cups, will have a read of it today


Bookmarks

Posting Permissions

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