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

Connected successfully but not data insert.



global $conn;

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



include "config.php";

$sth=$conn->prepare('INSERT INTO bilgiler(Ad,Mobilno,Evno,Eposta,Adres) VALUES(?,?,?,?,?)');
//echo "İşlem başarılı";

$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) */
/* throw exceptions on errors (default: stay silent) */
/* fetch associative arrays (default: mixed arrays)    */

    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.



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.