致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000]: General error' in [英] Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in

查看:164
本文介绍了致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000]: General error' in的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,它不断发出错误致命错误:未捕获的异常 'PDOException',消息为 'SQLSTATE[HY000]:一般错误' in..."该错误将我定向到$row"行= $q2->fetchAll(PDO::FETCH_OBJ);".我已经搜索了大量的解决方案,但无济于事.我的代码似乎与 php 文档中给出的示例格式相同...

I have this function and it keeps giving out the error "Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in..." The error is directing me to the line "$row = $q2->fetchAll(PDO::FETCH_OBJ);". I've searched tonnes for a solution but to no avail. My code appears to be the same format as the examples given in the php docs...

这是根据 TML 的建议更新的功能:

Here's the function updated as per TML's suggestions:

//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 输出:

var_dump output:

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)

在此先感谢您的帮助.

推荐答案

编写上述代码的更好方法 - 并且可能会解决您的问题 - 可能如下所示:

A better way to write the code above - and one that will likely fix your problem - might look something like this:

//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. "or die()" 留下了糟糕的用户体验 - 更优雅地处理错误.我在这里的示例中使用了异常处理,但这当然不是唯一的方法;因为你的 setAttribute 调用,我只是默认了.
  4. 虽然我在这里保留了您的全局变量,但您真的应该考虑不再使用全局",因为它通常被认为是非常糟糕的做法.谷歌的一点工作应该会出现许多讨论原因的文章,但迪米特法则是一个很好的起点.
  5. 所有这些USE"调用都没有理由;PDO 对象已经为您携带了该信息.

Freenode 的 ##PHP 的成员为您可能想要检查的 PDO 整理了一个教程在进一步发展之前退出.

The members of Freenode's ##PHP have put together a tutorial for PDO that you might want to check out before progressing too much further.

这篇关于致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000]: General error' in的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆