i am actually started using PDO , i have seen many questions on PDO but i am not able resolve my problem by reading those questions.
I have one file PDO Config File called db.php , this file having no problem.
db.php
<?php
######## PDO Config File ##########
$mysql_hostname = "localhost";
$mysql_user = "web";
$mysql_password = "123123";
$mysql_database = "123123";
//$odb = new PDO ("mysql:host=".$mysql_hostname.";dbname=".$mysql_database;charset:UTF-8",$mysql_user,$mysql_password);
try{
$connect = new PDO("mysql:host=".$mysql_hostname.";dbname=".$mysql_database.";charset:UTF-8", $mysql_user, $mysql_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe){
die('Could connect to the database because: ' .$pe->getMessage());
}
?>
I have Included this file in to the following PHP file[setting.php] .
setting.php
class Account
{
public function userEmailChnage($inputCurrentEmail,$inputChangeEmail,$inputConfirmEmail)
{
if(!empty($inputCurrentEmail) AND !empty($inputChangeEmail) AND !empty($inputConfirmEmail)){
$selectEmail = "select mail from users where mail = ?";
$selectEmailPrepare = $connect -> prepare($selectEmail);
$selectEmailPrepare -> execute(array($inputConfirmEmail));
if ($selectEmailPrepare ->rowCoun() > 0 ) {
if($inputChangeEmail == $inputConfirmEmail) {
$EmailUpdate = "UPDATE users SET mail= ?";
$EmailUpdatePrepare = $connect->prepare($EmailUpdate);
$EmailUpdatePrepare -> execute(array($inputChangeEmail));
$msg[EmailUpdateOpration]=Success;
}else{
$msg[IsConfirmMailMatching]=FALSE; //---------------- > JSON ERROR Msg
}
}else{
$msg[IsEmailInDatabase]=FALSE; //---------------- > JSON ERROR Msg
}
}else{
$msg[IsEmailFieldEmpty]=TRUE; //---------------- > JSON ERROR Msg
}
header("Content-Type: application/json", true);
echo json_encode($msg);
}
}
After the execution of the file i am facing following error :
Fatal error: Call to a member function prepare() on a non-object in /home/admin/public_html/class/setting.php on line 12
I am tried to change PDO config file , it didn’t worked. I started testing setting.php .. Not able to solve it !
In this line:
… you get this error:
That means exactly that:
$connectis not an object. It appears to be a global variable so you need to handle it as such: either callglobal $connect;before you use it or read it through$GLOBALS['connect']. (Or, even better, pass it as argument to the method.)