2012-12-19 63 views
-4

我實際上開始使用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 ..無法解決它!

+0

'$ connect'必須是'PDO'對象,然後才能在'userEmailChnage()'函數中使用它。 nitpick ..請將'userEmailChnage'改正爲'userEmailChange',以避免混淆拼寫。 – tradyblix

+0

[在獨立函數中訪問另一個類的方法]可能的重複(http://stackoverflow.com/questions/13932191/accessing-another-class-method-from-within-a-standalone-function) – deceze

+0

我有將該文件包含到setting.php中,因此它是對象! –

回答

2

在這一行:

$EmailUpdatePrepare = $connect->prepare($EmailUpdate); 

...你得到這個錯誤:

Call to a member function prepare() on a non-object

這意味着正是:$connect的對象。它似乎是一個全局變量,因此您需要像這樣處理它:在使用它之前請致電global $connect;或通過$GLOBALS['connect']讀取它。 (或者,甚至更好,將它作爲參數傳遞給方法。)

0

看來$connect不是PDO的對象。這可能是一些NULL或錯誤的值。 可能你的連接失敗了。嘗試檢查拋出PDOException:

try { 
    // pdo connection 
} catch (PDOException $e) { 
    exit($e->getMessage()); 
} 

公告稱,PDO::__construct()只在成功返回一個對象,並以其他方式thows異常。

0

您應該在userEmailChnage()方法中包含/需要db.php。

+0

寧願傳遞對Account類成員變量的引用,並在類中使用此引用。 –

+0

我已經收錄了! –

相關問題