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

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
anvd
  • 3,997
  • 19
  • 65
  • 126
  • `PDO` as a `quote` function to escape values, is it intended that you use `mysql_real_escape_string` instead? – Laimoncijus Mar 17 '11 at 22:46
  • yes is the intention. what is the function? thanks – anvd Mar 17 '11 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? – Laimoncijus Mar 17 '11 at 22:54

8 Answers8

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).

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    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) – anvd Mar 17 '11 at 22:50
  • 1
    @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. – user229044 Sep 05 '12 at 20:16
  • @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 – Giant Oct 07 '14 at 04:29
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
}
M Alam Telot
  • 367
  • 3
  • 9
2

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

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
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();
}

?>
Humphrey
  • 2,659
  • 3
  • 28
  • 38
1

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

slfan
  • 8,950
  • 115
  • 65
  • 78
sebastian
  • 11
  • 3
0

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

no92
  • 129
  • 2
  • 13
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;
} 
ketan
  • 19,129
  • 42
  • 60
  • 98
Sanner
  • 1
0

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

so, on line 41,

$stmt = null;
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jordan
  • 9
  • 1