简体   繁体   English

需要帮助以查找错误SQLSTATE [HY000]:常规错误

[英]Need help to find error SQLSTATE[HY000]: General error

I am inserting a row into the database. 我正在向数据库中插入一行。 It is working perfectly but when I enable PDO error message, it give me this error. 它工作正常,但是当我启用PDO错误消息时,它给了我这个错误。 I want to know exactly why this error coming so I can keep error messaging enabled during development. 我想确切知道为什么会出现此错误,以便在开发过程中保持启用错误消息的功能。 Here is my code: 这是我的代码:

Enabling error I put in my DB class 启用我放入数据库类的错误

$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Database connection in my class 我班上的数据库连接

try {
    $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
    $this->_prefix = '';
} catch (PDOException $e) {
    die($e->getMessage());
}

Here is the method where I call the query 这是我调用查询的方法

public function query($sql, $params = array()) {
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)) {
        $x=1;
        if(count($params)){
            foreach($params as $param){
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()){
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error= true;
        }

    }

    return $this;
} 

This is the insert function in my DB class 这是我的数据库类中的插入函数

  public function insert($table, $fields = array()){
    $keys = array_keys($fields);
    $values = '';
    $x = 1;

    foreach ($fields as $field) {
        $values .= "?";
        if ($x < count($fields)) {
            $values .= ', ';
        }
        $x++;
    }
    $sql = "INSERT INTO {$table} (`" . implode('`,`', $keys) . "`) VALUES ({$values})";

    if (!$this->query($sql, $fields)->error()) {
        return true;
    }

    return false;
}

This is the function in my tour class which saves all data to DB by calling insert function. 这是我的游览类中的函数,该函数通过调用insert函数将所有数据保存到DB。

public function create($fields= array()){
    if(!$this->_db->insert("tours", $fields)){
        throw new Exception('There was a problem creating Tours.' .print_r($this->_db->error()));
    }
} 

The reason to keep ATTR_ERRMODE enable is because it help me to debug during my development and I have still many pages to develop. 保持启用ATTR_ERRMODE的原因是因为它可以帮助我在开发过程中进行调试,并且仍有许多页面需要开发。 I have already visited many similar questions but they are related to error. 我已经访问过许多类似的问题,但它们与错误有关。 I don't normally get any errors but only when I enabled this error messaging system which tells me everything in detail. 我通常不会收到任何错误,只有在启用此错误消息传递系统后才能得到,该错误消息系统会告诉我所有详细信息。

This is different from other question as it is not giving me error but it insert row successfully but when I enable error mode that it give me error. 这不同于其他问题,因为它没有给我错误,但它成功插入行,但是当我启用错误模式时,它给了我错误。 Another I am using dynamic parameters so can't add colon : to rectify it. 另一个我正在使用动态参数,因此无法添加冒号:来纠正它。 As same function used to query database. 与查询数据库的功能相同。

My humble apologies to Aynber, as he was right I could not understand answer I have to remove fetchAll from my function. 我对Aynber表示歉意,因为他是对的,我无法理解答案,因此必须从函数中删除fetchAll。 So I have created now two function one for insertion/updating and another for just query. 因此,我现在创建了两个函数,一个用于插入/更新,另一个用于查询。 As answer was there but I was seeing other answers. 答案在那里,但我看到了其他答案。

I just removed following line from my query function 我刚刚从查询功能中删除了以下行

$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);

Thanks now my class is good as it is doing proper error handling. 现在,我的课程很好,因为它正在执行正确的错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM