İlker Ekşi İlker Ekşi - 1 year ago 78
MySQL Question

Call to a member function prepare()

$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>");


public function fetch_data($data){
global $pdo;
$query = $pdo->prepare($data);
return $query->fetch(PDO::FETCH_ASSOC);

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.

Answer Source

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.


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: