-2
<?php
date_default_timezone_set('Europe/Istanbul');
try{
$db_host = 'localhost';
$db_name = 'db_name';
$db_kadi = 'root';
$db_sifre = '';

$pdo = new PDO("mysql:host=".$db_host."; dbname=".$db_name, $db_kadi, $db_sifre, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}catch(PDOException $hata){
    if($hata->getCode() == "1044"){
        die("<h4>Sistem Bağlantı Hatası</h4><p>Sistem bağlantısı yok !</p>");
    }else if($hata->getCode() == "1049"){
        die("<h4>Database Sistem Hatası</h4><p>Database bağlantısı yok !</p>");
    }
}

?>

<?php
        public function fetch_data($data){
        global $pdo;
        $query = $pdo->prepare($data);
        $query->closeCursor();
        $query->execute();
        return $query->fetch(PDO::FETCH_ASSOC);
        $query->null;
    }
?>

Fatal error: Call to a member function prepare() on null in

I do not understand how I got to this error. I've tried almost everything I encounter the same problem. If you have friends that will help me with this please write. Thank you

PDO module active, installed mysqli a stronge error.

3 Answers3

0

The reason this is happening is because $data is null. When preparing the statement here ~>

$query = $pdo->prepare($data);

The data from your constructor is null. So go through and make sure wherever fetch_data is being called that the value you're passing isn't null.

EDIT:

It's because PDO is null. There must be something with how you're setting it up, as the global reference should give you access to the PDO.

Use this as a reference: http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Community
  • 1
  • 1
James111
  • 14,108
  • 14
  • 74
  • 116
  • it's because `$pdo` is null, not `$data` – ElefantPhace Aug 12 '16 at 01:46
  • No data problems "SELECT * FROM site_options .. .. .. ." – İlker Ekşi Aug 12 '16 at 01:55
  • Your PDO object ($pdo) is null, you must make sure that it's using the right connection properties @İlkerEkşi - Which is why I linked this tutorial -> http://www.w3schools.com/php/php_mysql_prepared_statements.asp – James111 Aug 12 '16 at 01:56
  • That means there is an error when connecting to mysql -> refer to this question: http://stackoverflow.com/questions/1435445/error-on-creating-connection-to-pdo-in-php – James111 Aug 12 '16 at 02:06
0

Well, first off, the error is that $pdo (even though you are using the global scope) is null. In your try, you are setting the $pdo variable, but if it fails, $pdo is never set. If $hata->getCode() doesn't return "1044" or "1049", the thread never dies.

Double check your connection string. Can you make a connection to it from your PC? I'm a little bit suspect of the '' password.

Eddimull
  • 332
  • 1
  • 8
0

Fatal error: Call to a member function prepare() on null

Here's the explanation of that error:

$pdo->prepare($data);

You can call a method from $pdo only if $pdo is an object of a class with that method. In this case, $pdo is null, which is no type of object. A null has no methods, so it's a fatal error to try to call any method on a null.

The cause of this problem is that when you get a PDO connection, you're checking only error codes 1044 and 1049. If any other error code is the cause of the exception, then your code does not call die(), it proceeds to the second block of code.

But then $pdo still has not been set to a valid database connection!

It's like if you call someone on the phone, but your mobile phone has no signal. But you start talking to your friend anyway?!?

If the code in the catch() block is executing, it means something went wrong, and new PDO() was not successful. You must call die() in the catch() block one way or the other.

To fix this, I would recommend the following. In your catch() block, make sure you call die() by the end of the block, no matter what the error code is (example below).

I also recommend that you save the exception message into your error log. This is to help you troubleshoot. Don't display the error to users in the HTML output, because they don't know what that error means anyway. But you can look for it in your PHP error log (which is typically your Apache error log).

<?php
date_default_timezone_set('Europe/Istanbul');
try{
    $db_host = 'localhost';
    $db_name = 'db_name';
    $db_kadi = 'root';
    $db_sifre = '';
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_kadi, $db_sifre, 
        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch (PDOException $hata) {
    error_log($hata->getMessage()); // log the error!
    if ($hata->getCode() == "1044") {
        die("<h4>Sistem Bağlantı Hatası</h4><p>Sistem bağlantısı yok !</p>");
    } else if ($hata->getCode() == "1049") {
        die("<h4>Database Sistem Hatası</h4><p>Database bağlantısı yok !</p>");
    }
    // make sure to call die() if any other error code occurs!
    die("<h4>Database Sistem Hatası</h4><p>Veritabanı yöneticisine başvurun !</p>"
}

?>

Once you do this, you can guarantee either $pdo is valid, or else the code has called die() and will not try to use an invalid $pdo.

Bill Karwin
  • 499,602
  • 82
  • 638
  • 795