2013-02-11 115 views
3

我有這個函數並且它一直髮出錯誤「致命錯誤:帶有消息」SQLSTATE [HY000]的未捕獲異常'PDOException' :一般錯誤'in ...「錯誤指示我」$ row = $ q2-> fetchAll(PDO :: FETCH_OBJ);「。我已經搜索噸解決方案,但無濟於事。我的代碼似乎是相同的格式,在PHP文檔中給出的例子......致命錯誤:帶有消息'SQLSTATE [HY000]:一般錯誤'的未捕獲的異常'PDOException'

這裏的更新每TML的建議功能:

//gets a record by id and sets object properties to it's values 
function getById($sid) { 
    global $conf, $pdo; 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    //checks to see if a record exists for the given id 
    try { 
     $stmt = $pdo->prepare('Use ' . $conf['database'] . '; select mem_id as "_id", mem_name as "_name", mem_info as "_info", 
           mem_password as "_password", mem_email as "_email", mem_image as "_image", 
           mem_group as "_group" 
           from ' . $conf['prefix'] . 'members 
           where mem_id = ?;'); 
     echo"85 <br />"; 
     $stmt->execute(array($sid)); 
     echo"86 <br />"; 
     $rows = $stmt->fetchAll(PDO::FETCH_OBJ); 
     echo"90 <br />"; 
     print_r($rows); 
     if (count($rows) !== 1) { 
      throw new Exception("Some exception here"); 
     } 
     foreach($rows[0] as $field=>$value) { 
      $this->$field = $value; 
      echo"97 <br />"; 
     } 
    } catch (PDOException $e) { 
     echo"something went wrong! " . var_dump($e); 
    } 
} 

的var_dump輸出:

object(PDOException)[4] 
    protected 'message' => string 'SQLSTATE[HY000]: General error' (length=30) 
    private 'string' (Exception) => string '' (length=0) 
    protected 'code' => string 'HY000' (length=5) 
    protected 'file' => string 'D:\wamp\www\testing\scripts\Kantan\classes\Member.php' (length=53) 
    protected 'line' => int 86 
    private 'trace' (Exception) => 
    array (size=2) 
     0 => 
     array (size=6) 
      'file' => string 'D:\wamp\www\testing\scripts\Kantan\classes\Member.php' (length=53) 
      'line' => int 86 
      'function' => string 'fetchAll' (length=8) 
      'class' => string 'PDOStatement' (length=12) 
      'type' => string '->' (length=2) 
      'args' => 
      array (size=1) 
       ... 
     1 => 
     array (size=6) 
      'file' => string 'D:\wamp\www\testing\scripts\Kantan\test.php' (length=43) 
      'line' => int 5 
      'function' => string 'getById' (length=7) 
      'class' => string 'Member' (length=6) 
      'type' => string '->' (length=2) 
      'args' => 
      array (size=1) 
       ... 
    private 'previous' (Exception) => null 
    public 'errorInfo' => 
    array (size=1) 
     0 => string 'HY000' (length=5) 
    public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> PDOException: SQLSTATE[HY000]: General error in D:\wamp\www\testing\scripts\Kantan\classes\Member.php on line <i>86</i></th></tr> 
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> 
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeee'... (length=1472) 

謝謝提前尋求幫助。

+0

代碼中的$ q2-> fetchAll'在哪裏? – Barmar 2014-08-05 05:29:23

回答

1

一種更好的方式來寫上面的代碼 - 而這將有可能解決您的問題 - 可能是這個樣子:

//gets a record by id and sets object properties to it's values 
function getById($sid) { 
    global $conf, $pdo; 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    //checks to see if a record exists for the given id 
    try { 
     $stmt = $pdo->prepare('select mem_id as "_id", mem_name as "_name", mem_info as "_info", 
           mem_password as "_password", mem_email as "_email", mem_image as "_image", 
           mem_group aS "_group" 
           from members 
           where mem_id = ?'); 
     $stmt->execute(array($sid)); 

     $rows = $stmt->fetchAll(PDO::FETCH_OBJ); 
     if (count($rows) !== 1) { 
      throw new Exception("Some exception here"); 
     } 
     foreach($rows[0] as $field=>$value) { 
      $this->$field = $value; 
     } 
    } catch (PDOException $e) { 
     /* handle errors in useful way, don't just die() */ 
    } 
} 

一些需要注意的差異:

  1. 有不似乎是任何兩次查詢數據庫的理智原因。
  2. 上面的代碼忽略了使用PDO準備好的語句的一個主要好處 - 即對查詢進行參數化。
  3. 「或die()」留下糟糕的用戶體驗 - 更優雅地處理錯誤。我在這裏使用了異常處理,但這當然不是唯一的方法;我只是默認那是因爲你的setAttribute調用。
  4. 雖然我在這裏完整地保留了你的全局變量,但你應該考慮放棄使用'全局',因爲它通常被認爲是很差的做法。谷歌的一些工作應該發表任何文章來討論爲什麼,但是德米特法是一個很好的起點。
  5. 沒有理由給所有'USE'電話; PDO對象已經爲您提供了這些信息。

Freenode上的## PHP的成員已經把一個tutorial爲PDO,你可能要進步太多了進一步的前退房。

+0

非常感謝您的回覆,我現在無法嘗試,因爲我必須上大學,但我一定會在我回家時嘗試一下。我不想使用全局變量,但是如果我不這樣做,這個函數就不能看到這些變量,我會盡量在後面解決這個問題! – Jamal 2013-02-11 10:41:09

+0

我試過了,雖然我現在有更好的代碼,但同樣的錯誤仍然存​​在...... – Jamal 2013-02-11 17:39:27

+0

你現在可以用你的代碼狀態更新問題嗎?另外,如果您的'catch'語句中沒有正文,請將其'var_dump($ e);'至少包括結果。 – TML 2013-02-11 18:04:28

相關問題