我實際上開始使用PDO,我已經看到了很多關於PDO的問題,但我無法通過閱讀這些問題來解決我的問題。PDO Prepare()函數致命錯誤:
我有一個文件PDO配置文件調用db.php
,這個文件沒有問題。
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());
}
?>
我已將此文件包含到以下PHP文件[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);
}
}
我面臨着以下錯誤的文件執行後:
Fatal error: Call to a member function prepare() on a non-object in /home/admin/public_html/class/setting.php on line 12
我試圖改變PDO的配置文件,它並沒有奏效。我開始測試setting.php ..無法解決它!
'$ connect'必須是'PDO'對象,然後才能在'userEmailChnage()'函數中使用它。 nitpick ..請將'userEmailChnage'改正爲'userEmailChange',以避免混淆拼寫。 – tradyblix
[在獨立函數中訪問另一個類的方法]可能的重複(http://stackoverflow.com/questions/13932191/accessing-another-class-method-from-within-a-standalone-function) – deceze
我有將該文件包含到setting.php中,因此它是對象! –