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)

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

and insert/check then insert query


public function add()
		{	
			$query['1'] = 'SELECT COUNT(*) FROM users WHERE email = :email';
			$values['1'] = array('email' =&gt; $this-&gt;email);
			$check = $pdo-&gt;prepare($query['1']);
						
			if($res = $check-&gt;execute($vlaues['1']))
				{
					if($check-&gt;fetchColumn() &lt; 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' =&gt; $this-&gt;email, 'password' =&gt; $this-&gt;password, 'farmname' =&gt; $this-&gt;farmname, 'name' =&gt; $this-&gt;farm, 'salt1' =&gt; $salt['2'], 'salt2' =&gt; $salt['1'], 'reciveEmail' =&gt; $this-&gt;reciveEmail );
			
							$insert = $pdo-&gt;prepare($query['2']);
							$insert-&gt;execute($values['2']);


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

														
						}

do you see anything i am missing?

Thanks!
-Colin

You need to declare your $pdo variable as a global within the function itself, see the below.

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

Using the global keyword within an object…that is just bad voodoo…


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.

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?


// 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 [google]prefer composition over inheritance[/google] which is the example logic_earth showed you.

Off Topic:

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