我正在尝试使用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
的原因是什么?