PHP PDO - PDOException'一般错误',即使查询成功

时间:2016-04-13 14:55:59

标签: php mysql oop model-view-controller pdo

我正在尝试使用PDO在MySQL表table中插入一行。插入行,但PDO仍然会引发以下错误:

  

PDOException:SQLSTATE [HY000]:第39行的C:\ wamp64 \ www \ index.php中的常规错误

这是代码:

<?php
    class DBCon {
        private static $instance = null;
        private $db;

        private function __construct() {
            $this->db = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

        public static function getDB() {
            if (self::$instance === null) {
                self::$instance = new self();
            }
            return self::$instance->db;
        }
    }

    class Item {
        private $parameter1;
        private $parameter2;
        private $parameter3;
        private $parameter4;

        function __construct($parameter1, $parameter2=null, $parameter3=null, $parameter4=null) {
            $this->parameter1 = $parameter1;
            $this->parameter2 = $parameter2;
            $this->parameter3 = $parameter3;
            $this->parameter4 = $parameter4;
        }

        function createItem($extra1, $extra2, $extra3) {
            $query = DBCon::getDB()->prepare('INSERT INTO `table`(`id`, `text`, `int`, `bool`) VALUES (:id,:extra1,:extra2,:extra3)');
            $query->bindValue(':id', $this->parameter1, PDO::PARAM_INT);
            $query->bindValue(':extra1', $extra1, PDO::PARAM_STR);
            $query->bindValue(':extra2', $extra2, PDO::PARAM_INT);
            $query->bindValue(':extra3', $extra3, PDO::PARAM_INT);
            $query->execute();
            return $query->fetch(PDO::FETCH_ASSOC); //line 39
        }
    }

    $item = new Item('600');
    echo $item->createItem('some text', 24, true);

导致General error的原因是什么?

0 个答案:

没有答案