致命错误:未捕获的PDOException:SQLSTATE [HY000]:常规错误

时间:2020-06-08 03:41:26

标签: php oop

帮我解决这个问题:

致命错误:未捕获的PDOException:SQLSTATE [HY000]:中的常规错误 C:\ laragon \ www \ oop \ system \ Core \ Database.php:39堆栈跟踪:#0 C:\ laragon \ www \ oop \ system \ Core \ Database.php(39): PDOStatement-> fetchAll(5)#1 C:\ laragon \ www \ oop \ system \ Core \ Database.php(62): WeTry \ Core \ Database-> query('INSERT INTO use ...',数组)#2 C:\ laragon \ www \ oop \ app \ Controllers \ Home.php(13): WeTry \ Core \ Database-> insert('users',Array)#3 [内部功能]: CodeGo.net> index()#4 C:\ laragon \ www \ oop \ system \ Core \ App.php(32): call_user_func_array(Array,Array)#5 C:\ laragon \ www \ oop \ public \ index.php(14):Wetry \ Core \ App :: run()#6 {main}在C:\ laragon \ www \ oop \ system \ Core \ Database.php中抛出 39

以及来自类数据库的这段代码

<?php
    namespace WeTry\Core;
    use PDO;
    class Database {

        private static $_instance = null;
        private $pdo, $query, $error = false, $result, $count = 0, $lastInsertId = null;

        private function __construct() {
            $database = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME;
            $option = [
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ];
            try {
                $this->pdo = new PDO($database, DB_USER , DB_PASS, $option);
            } catch (PDOException $e) {
                die($e->getMessage());
            }
        }
        public static function run() {
            if(!isset(self::$_instance)) {
                self::$_instance = new Database();
            } else {
                return self::$_instance;
            }
        }
        public function query($sql, $params = []) {
            $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->result = $this->query->fetchAll(PDO::FETCH_OBJ);
                    $this->count = $this->query->rowCount();
                    $this->lastInsertId = $this->pdo->lastInsertId();
                } else {
                    $this->error = true;
                }
            }
            return $this;
        }

        public function insert($table, $fields = []) {
            $fieldString = '';
            $valueString = '';
            $values = [];

            foreach ($fields as $field => $value) {
                $fieldString .= '`' . $field . '`,';
                $valueString .= '?,';
                $values[] = $value;
            }
            $fieldString = rtrim($fieldString, ',');
            $valueString = rtrim($valueString, ',');
            $sql = "INSERT INTO {$table} ({$fieldString}) VALUES ({$valueString})";
            if(!$this->query($sql, $values)->error()) {
                return true;
            }
            return false;
        }

        public function error() {
            return $this->error;
        }
    }

如何解决?

0 个答案:

没有答案