dongliehuan3925 2016-08-08 15:59
浏览 63

需要帮助才能找到错误SQLSTATE [HY000]:常规错误

I am inserting a row into the database. It is working perfectly but when I enable PDO error message, it give me this error. 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.

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. 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.

  • 写回答

1条回答 默认 最新

  • dormbaf90464 2016-08-09 05:25
    关注

    My humble apologies to Aynber, as he was right I could not understand answer I have to remove fetchAll from my function. 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看