PHP error: "Fatal error: Call to a Member Function prepare()"

Asked

Viewed 13,878 times

0

When I try to execute the SELECT server accuses this msg: Fatal error: Call to a member function prepare() on a non-object in blog.php on line 97 the line referred to is:

$readPost = $db->prepare($postagemRead);

a little more like this so:

$pg = 1;
    $limite = 5;
    $inicio = ($pg * $limite) - $limite;

    $postagemRead = "SELECT * FROM postagem ORDER BY datapost DESC LIMIT :inicio,:limite";
    try{
            $readPost = $db->prepare($postagemRead);

the connection file is this:

<?php 
try{
        $db = new PDO("mysql:dbname=database;host=localhost;charset=utf8;","root","");
    }catch(PDOException $e){
        $e -> getMessage();
    }   
?>

do not see the problem seen running this on the local server(wamp server) and runs normal, but on the host of that error what may be?

Error:

Exception 'Pdoexception' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)' in /home/vhosts/molbe.freetzi.com/config.php:3 Stack trace: #0 /home/vhosts/molbe.freetzi.com/config.php(3): PDO->__Construct('mysql:dbname=85...', 'root', ') #1 /home/vhosts/molbe.freetzi.com/padrao.php(1): require_once('/home/vhosts/mo...') #2 {main}' in /home/vhosts/molbe.freetzi.com/config.php:5 Stack trace: #0 /home/vhosts/molbe.freetzi.com/padrao.php(1): require_once() #1 {main} thrown in /home/vhosts/molbe.freetzi.com/config.php on line 5

  • The line $e->getMessage() does absolutely nothing within that catch. You need to put an echo/die or do some treatment on catch, otherwise the code will continue running normally even if there is some error in the connection.

  • Error 'Exception 'Pdoexception' with message 'SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)' in /home/vhosts/molbe.freetzi.com/config.php:3 Stack trace: #0 /home/vhosts/molbe.freetzi.com/config.php(3): PDO->_Construct('mysql:dbname=85...', 'root', ') #1 /home/vhosts/molbe.freetzi.com/padrao.php(1): require_once('/home/vhosts/mo...') #2 {main}' in /home/vhosts/molbe.freetzi.com/config.php:5 Stack trace: #0 /home/vhosts/molbe.freetzi.com/padrao.php(1): require_once() #1 {main} thrown in /home/vhosts/molbe.freetzi.com/config.php on line 5

2 answers

2

  1. Connection object not saved in variable $db. Therefore, the function prepare does not exist in $db. This is because...

  2. ...you are not exploding the error, ie the try {} catch() is not stopping your code and it’s continuing, giving the illusion that $db exists.

    Correct:

    try
    {
        $db = new PDO("mysql:dbname=database;host=localhost;charset=utf8;","root","");
    }
    catch(PDOException $e)
    {
        throw new PDOException($e);
    } 
    

After exploding the error, you will see which message on connection failure in new PDO. I don’t see any more errors, the variable $db will exist correctly and therefore, $db->prepare($postagemRead); will work.

Tip

Probably the user root and the password "" do not exist in the host (remote server).

  • 3

    What’s the sense of throwing the same exception you just captured?

  • removed the root and "" $db = new PDO("mysql:dbname=database;host=localhost;charset=utf8;"); but still of error

  • I switched only to print $e and gave this msg: Exception 'Pdoexception' with message 'SQLSTATE[28000] [1045] Access denied for user 'apache'@'localhost' (using password: NO)' in /home/vhosts/molbe.freetzi.com/config.php:3 Stack trace: #0 /home/vhosts/molbe.freetzi.com/config.php(3): PDO->__Construct('mysql:dbname=85...') #1 /home/vhosts/molbe.freetzi.com/padrao.php(1): require_once('/home/vhosts/mo...') #2 {main}

  • 2

    @Hebertdelima, it is not to "remove", it is to fix, because in your remote server the user and password are very likely different. You pasted the try {... } that I put in the post? It launches the corresponding error and you see what the problem is in the connection.

  • I’ll check with the host.

-2

Try using a RETURN after instantiating the connection, like this:

$db = new PDO("mysql:dbname=database;host=localhost;charset=utf8;","root","");
return $db;

if the connection is in a class and you instate this class, create a public variable $Conn for example, this way:

class Conexao {

    public $conn;

    try{
        $this->conn = "aqui sua linha de conexao";
        return $this->conn;
    }catch(PDOException $e){
        echo $e->getMessage();
    }
}

hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.