PHP PDO Common Error i Faced -


i gotten error

database connected fatal error: call member function prepare() on non-object in d:\home\site\wwwroot\databasemethods.php on line 22  

i can't seem find causing 1 point out whats wrong please enlighten me.

i have tried call connection before running prepared statement cant seems work or should

thanks in advance

databasemethods.php

    ini_set('display_errors', 1);     ini_set('display_startup_errors', 1);     error_reporting(e_all);      require_once("databaseconnection.php");      date_default_timezone_set("asia/singapore");      //register      function registeruser($email , $password)     {      $conn = connect_db();      $stmt = null;      $stmt = $conn->prepare("insert secure_login (email,password,created_dt) values(?,?,?)"); //error line      $stmt->execute(array($email, $password , date("y-m-d h:i:s")));       if( $stmt )          {             return "success";         }     else         {             return "failed";         }     }      //date("y-m-d h:i:s")      //end register     ?> 

databaseconnection.php

<?php      function connect_db()     {     $servername = "xxxx";     $username = "xxx";     $password = "xxx";      try {         $conn = new pdo("mysql:host=$servername;dbname=xxx", $username, $password);         // set pdo error mode exception         $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);         echo "database connected successfully";          }     catch(pdoexception $e)         {         echo "connection failed: " . $e->getmessage();         }     }      ?> 

answer

<?php  function connect_db() { $servername = "xxx"; $username = "xxx"; $password = "xxx";  try {     $conn = new pdo("mysql:host=$servername;dbname=xxx", $username, $password);     // set pdo error mode exception     $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);     echo "database connected successfully";       return $conn;      } catch(pdoexception $e)     {     echo "connection failed: " . $e->getmessage();     } }  ?> 


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -