问题描述
所以我的代码运行得非常好,直到我尝试插入到数据库中.. 我的 DB.PHP(问题似乎出在这里,我检查了所有其他文件,没有任何问题导致任何问题,我使用的函数是 Insert 函数,在第 37 行出现错误:
<?PHP
class DB
{
private static $_instance = null;
private $_pdo,$_query,$_error = false,$_result,$_count = 0;
private function __construct()
{
try {
$this->_pdo = new PDO('MysqL:host=' . Config::get('MysqL/host') . ';dbname=' . Config::get('MysqL/db') . '',Config::get('MysqL/username'),Config::get('MysqL/password'));
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
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;
}
public function action($action,$table,$where = array())
{
if (count($where) == 3) {
$operators = array('=','>','<','>=','<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if (in_array($operator,$operators)) {
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if ($this->query($sql,array($value))) {
return $this;
}
}
}
return false;
}
public function get($table,$where)
{
return $this->action('SELECT *',$where);
}
public function delete($table,$where)
{
return $this->action('DELETE *',$where);
}
public function error()
{
return $this->_error;
}
public function count()
{
return $this->_count;
}
public function insert($table,$fields = array())
{
if (count($fields)) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field){
$values .= "?";
if($x < count($fields)){
$values .= ',';
}
$x++;
}
$sql = "INSERT INTO users (`" . implode('`,`',$keys) . "`) VALUES ({$values})";
echo $sql;
if(!$this->query($sql,$fields)->error()){ //where my error occurred
return true;
}
}
return false;
}
}
(数据已插入到表中,但该错误似乎至关重要,我在任何地方都没有找到任何相关信息) 我在 YouTube 上观看了一个教程,问题出现在这里 - https://youtu.be/zvXgsouIzVg?t=5258
(完整错误):
Fatal error: Uncaught PDOException: sqlSTATE[HY000]: General error in D:\xampp\htdocs\classes\DB.PHP:37 Stack trace: #0 D:\xampp\htdocs\classes\DB.PHP(37): PDOStatement->fetchAll(5) #1 D:\xampp\htdocs\classes\DB.PHP(93): DB->query('INSERT INTO use...',Array) #2 D:\xampp\htdocs\test.PHP(4): DB->insert('users',Array) #3 {main} thrown in D:\xampp\htdocs\classes\DB.PHP on line 37
提前致谢,这对我很有帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)