16

This code get an error:

Fatal error: Call to a member function prepare() on a non-object in C:\Users\fel\VertrigoServ\www\login\validation.php on line 42

CODE:

 function repetirDados($email) { if(!empty($_POST['email'])) { $query = "SELECT email FROM users WHERE email = ?"; $stmt = $pdo->prepare($query); // error line: line 42 $email = mysql_real_escape_string($_POST['email']); $stmt->bindValue(1, $email); $ok = $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($results == 0) { return true; } else { echo '<h1>something</h1>'; return false; } } } 

What is the possible cause? Another question, What is the equivalent to mysql_num_rows? sorry, I am newbie with pdo

3
  • PDO as a quote function to escape values, is it intended that you use mysql_real_escape_string instead? Commented Mar 17, 2011 at 22:46
  • yes is the intention. what is the function? thanks Commented Mar 17, 2011 at 22:48
  • 1
    Furthermore - I think PDOStatement::bindValue does quote value automatically (the same way as PDOStatement::execute does), so my guess is that you don't need to escape it again? Commented Mar 17, 2011 at 22:54

8 Answers 8

23

$pdo is undefined. You're not declaring it inside the function, and it isn't being passed in as an argument.

You need to either pass it in (good), or define it in the global namespace and make it available to your function by placing global $pdo at the top (bad).

Sign up to request clarification or add additional context in comments.

3 Comments

the most part of examples always put in the top new pdo and the connection settings, i think this is what you call (bad). And the alternative? a (good)
@Fel Using global variables is generally considered a bad practice. Suffice it to say, they lead to code which is extremely difficult to maintain and very error-prone. Any code which suggests using a global $pdo object is probably meant to be sample code only. You certainly shouldn't design your program so that a global $pdo object is how many different parts of your code are accessing the database.
@meagar what do you suggest i do to not make it a global variable an example would make it easier for me to understand what you mean
3

You can make a function of database connection in the same php page & call that function whenever you want. As,

public function connection() { $dbc = new PDO("mysql:host=localhost;dbname=chat","root",""); } public function1() { this->connection(); // now you have the connection.. now, time for to do some query.. } public function2() { this->connection(); // now do query stuffs.. } 

Or simply you can simply write the database connection line in that page every time when you need it. As,

public function a() { // connecting DB for this function a only... $dbc = new PDO("mysql:host=localhost;dbname=chat","root",""); // bla bla bla... } public function b() { // connecting DB for this function b only... $dbc = new PDO("mysql:host=localhost;dbname=chat","root",""); // abra ke dabra... boom } 

Comments

2

The $pdo object isn't in scope within your function.

Comments

1

@Anvd . I had the same problem but I did solve it by connecting the database in the same page not just to include the coonnecting page . It worked for me

<?php try { $pdo = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf-8','root',''); } catch(PDOException $e){ echo 'Connection failed'.$e->getMessage(); } ?> 

1 Comment

@Your Common Sense please hook me up with a right way
1

In regards the equivalent of mysql_num_rows in PDO is basically FETCH_NUM it return a index number of the selected row.

Comments

0

I was getting the same error: Then I saw I called my class after I had closed the PDO connection.

Comments

0

Yes, I also learned this the hard way, you need to open DB connection inside the function. I assumed the connection to the DB would be opened inside the function if I opened before calling the function, but no. So:

function whatever(){ //OPEN DB CONNECTION CODE //CLOSE DB return whateverValue; } 

Comments

0

You may also get this error from active, unbuffered queries still active.

so, on line 41,

$stmt = null; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.