İlker Ekşi İlker Ekşi - 3 months ago 12
MySQL Question

Call to a member function prepare()

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

Answer

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

Comments