Fatal error: Call to a member function prepare() on a non-object in

Connected successfully but not data insert.
config.php

<?php

$dbserver="localhost";
$dbuser="root";
$dbuserpass="";
$db="rehber";

global $conn;


try {
    $conn = new PDO("mysql:host=$dbserver;db=rehber", $dbuser, $dbuserpass);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
	}
    $conn = null;
?>

kaydet.php

<?php



include "config.php";
$ad=$_POST["Ad"];
$mobilno=$_POST["Mobilno"];
$evno=$_POST["Evno"];
$eposta=$_POST["Email"];
$adres=$_POST["Adres"];



$sth=$conn->prepare('INSERT INTO bilgiler(Ad,Mobilno,Evno,Eposta,Adres) VALUES(?,?,?,?,?)');
$ekle=$sth->execute(array($ad,$mobilno,$evno,$eposta,$adres));
//echo "İşlem başarılı";
//header("refresh:2;url-index.php");

$hata = $sth->errorInfo();
    echo empty($hata[2]) ?  "Başarılı Bir Şekilde Çalıştı." : $hata[2];
	



?>

See that $conn = null; line at the end of config.php? I suspect you meant to have that inside of your catch block.

1 Like

I don’t know why you’re using global variables in the first place, for you’re using a configuration file (config.php) for all you would have to do is something like -

    $pdo_options = [
/* important! use actual prepared statements (default: emulate prepared statements) */
PDO::ATTR_EMULATE_PREPARES => false
/* throw exceptions on errors (default: stay silent) */
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
/* fetch associative arrays (default: mixed arrays)    */
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ];

    try {
$conn = new PDO('mysql:host=' . \DATABASE_HOST . ';dbname=' . \DATABASE_NAME . ';charset=utf8', \DATABASE_USERNAME, \DATABASE_PASSWORD, $pdo_options);
    } catch (Exception $e) {
echo "Connection failed: " . $e->getMessage();
    }

Continuous error :smile:

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected’ in C:\wamp\www\rehber\kaydet.php on line 15
( ! ) PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in C:\wamp\www\rehber\kaydet.php on line 15

Your dsn is messed up. Should be dbname not db.

http://php.net/manual/en/pdo.construct.php

‘mysql:dbname=testdb;host=127.0.0.1’

I don’t think that’s how you are suppose to use the prepared statements. As far as I know it, the only library that uses question marks as placeholders is the MySQLi_ library (I could be wrong).

That could be the reason why your first error you are having is throwing you Fatal error: Call to a member function prepare() on a non-object in. It could also be a typo or mis-spelled field. Usually this is the case. That or the field doesn’t exist.

You can also use them in PDO but PDO also allows you to name the placeholders so most people do that rather than using the anonymous ones.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.