簡體   English   中英

致命錯誤:未捕獲異常'PDOException',消息'SQLSTATE [HY000]:常規錯誤'中

[英]Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in

我有這個功能,它不斷發出錯誤“致命錯誤:未捕獲異常'PDOException'消息'SQLSTATE [HY000]:一般錯誤'在......”錯誤指示我行“$ row = $ q2 - >使用fetchall(PDO :: FETCH_OBJ);”。 我搜索噸以尋求解決方案,但無濟於事。 我的代碼看起來與php文檔中給出的示例格式相同...

這是根據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)

在此先感謝您的幫助。

編寫上面代碼的更好方法 - 以及可能解決問題的代碼 - 可能看起來像這樣:

//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. “或者死亡()”留下了糟糕的用戶體驗 - 更優雅地處理錯誤。 我在這里的例子中使用了異常處理,但這當然不是唯一的方法; 由於你的setAttribute調用,我只是默認為那個。
  4. 雖然我在這里保留了你的全局變量,你應該考慮放棄使用“全局”,因為它通常被認為是非常糟糕的做法。 谷歌的一些工作應該發表任何數量的文章討論原因,但德米特法則是一個很好的起點。
  5. 所有那些'USE'電話都沒有理由; PDO對象已經為您提供了該信息。

Freenode ## PHP的成員已經為PDO編寫了一個教程 ,您可能希望在進一步學習之前查看它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM