gpt4 book ai didi

php - PDO 未插入 - 错误代码 00000

转载 作者:可可西里 更新时间:2023-11-01 07:58:10 31 4
gpt4 key购买 nike

我的 INSERT 查询有问题,$pdo->执行返回 false,错误代码为 00000

Query
string 'INSERT INTO module_test (img_name, description, priority) VALUES(:img_name, :description, :priority)' (length=100)

errorInfo() return:
array (size=3)
0 => string '00000' (length=5)
1 => null
2 => null

代码:

private function Init($query, $parameters = "")
{
# Connect to database
if (!$this->bConnected) {
$this->Connect();
}
try {
# Prepare query
$this->sQuery = $this->pdo->prepare($query);

# Add parameters to the parameter array
$this->bindMore($parameters);

# Bind parameters
if (!empty($this->parameters)) {
foreach ($this->parameters as $param => $value) {

$type = PDO::PARAM_STR;
switch ($value[1]) {
case is_int($value[1]):
$type = PDO::PARAM_INT;
break;
case is_bool($value[1]):
$type = PDO::PARAM_BOOL;
break;
case is_null($value[1]):
$type = PDO::PARAM_NULL;
break;
}
// Add type when binding the values to the column
$this->sQuery->bindValue($value[0], $value[1], $type);
}
}
# Execute SQL
var_dump($query);
var_dump($this->sQuery->execute());
var_dump($this->sQuery->errorInfo());
}
catch (PDOException $e) {
# Write into log and display Exception
echo $this->ExceptionLog($e->getMessage(), $query);
die();
}

# Reset the parameters
$this->parameters = array();
}
public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
$query = trim(str_replace("\r", " ", $query));

$this->Init($query, $params);

$rawStatement = explode(" ", preg_replace("/\s+|\t+|\n+/", " ", $query));

# Which SQL statement is used
$statement = strtolower($rawStatement[0]);
if ($statement === 'select' || $statement === 'show') {
return $this->sQuery->fetchAll($fetchmode);
} elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') {
return $this->sQuery->rowCount();
} else {
return NULL;
}
}
public function insert($table, $keyValue)
{
$fieldString = '';
$valueString = '';
$i = 1;
foreach ($keyValue as $key => $currKeyValue)
{
$fieldString .= $key;
$valueString .= ':'.$key;
if($i != count($keyValue))
{
$fieldString .= ', ';
$valueString .= ', ';
}
$i++;
}
$query = 'INSERT INTO '.$table.' ('.$fieldString.') VALUES('.$valueString.')';
$this->query($query, $keyValue);
}

参数数组

F:\Dev\wamp\wamp64\www\include\class\Database.class.php:216:
array (size=3)
'img_name' => string 'ttt1' (length=4)
'description' => string 'ttt1' (length=4)
'priority' => int 0

我已经在 phpmyadmin 中尝试过这个查询并且一切正常。

如果有人知道如何解决这个问题?

谢谢

PS:抱歉我的英语不好

最佳答案

据报道,在某些情况下,PDO 不会填充 errorInfo 属性。

相反,您必须让它抛出异常,这是获取错误消息的最可靠方法。为此,在您的构造函数中添加此行

$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

另请注意,您的类(class)是编写 PDO 包装器可能犯的所有错误的真实示例。我在一篇文章中整理了最流行的错误,Your first database wrapper's childhood diseases并且您的类(class)包含其中的每一个。

关于php - PDO 未插入 - 错误代码 00000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39830472/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com