PDO 与 JSON_INSERT 更新 table
PDO with JSON_INSERT to update table
我有一个 class 可以从 table 中插入、更新和删除项目,所以当使用 JSON_INSERT 时 class 不能正常工作,它给出以下错误:
Fatal error: Uncaught PDOException: SQLSTATE[22032]: <>: 3140 Invalid JSON text: "Missing a name for object member." at position 1 in value for column 'products.p_description'. in
/Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php:182 Stack trace: #0
/Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php(182): PDOStatement->execute() #1
/Library/WebServer/Documents/Tests/PHP/MySQL-JSON/json_update.php(30):
Database->execute() #2 {main} thrown in
/Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php on line 182
我在CRUD.php中使用的代码:
public function update($table, $fields = [], $where = null)
{
$args = [];
foreach ($fields as $fk => $fv) :
$fk = $this->strSafe($fk);
$args[] .= $fk . ' = :' . $fk;
endforeach;
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $args);
if ($where != null) {
$sql .= ' WHERE ' . $where;
}
$this->sql = $sql;
}
public function execute()
{
return $this->stmt->execute(); // LINE 182
}
要更新 table 字段“p_description”并添加值为“Qualcomm”的新项目“芯片组”,我使用了此代码 json_update.php:
$upMemList = [
'p_description' => "JSON_INSERT(`p_description` , '$.chipset' , 'Qualcomm')"
];
$upOn = $dbh->update('products', $upMemList, 'id = :id');
$dbh->prepare($upOn);
foreach ($upMemList as $fk => $fv) :
$dbh->bind(':' . $fk, $fv);
endforeach;
$dbh->bind('id', 1);
$dbh->execute(); // LINE 30
Table结构:
--
-- Table structure for table `products`
--
CREATE TABLE `products` (
`id` int UNSIGNED NOT NULL,
`p_name` varchar(50) DEFAULT NULL,
`p_description` json DEFAULT NULL,
`p_cat` int NOT NULL,
`p_price` int NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
--
-- Dumping data for table `products`
--
INSERT INTO `products` (`id`, `p_name`, `p_description`, `p_cat`, `p_price`) VALUES
(1, 'TV1', '{\"p_usb\": \"2\", \"p_hdmi\": \"3\", \"chipset\": \"Qualcomm\", \"p_screen\": \"21\", \"resolution\": null}', 1, 3400),
(2, 'TV2', '{\"p_usb\": \"3\", \"p_hdmi\": \"3\", \"p_screen\": \"43\", \"resolution\": null}', 1, 4500),
(3, 'Computer1', '{\"p_usb\": \"2\", \"p_hdmi\": \"0\", \"chipset\": \"Qualcomm\", \"p_screen\": \"15\", \"resolution\": null}', 4, 7350),
(4, 'TV3', '{\"ports\": {\"p_usb\": \"3\", \"p_hdmi\": \"2\"}, \"p_screen\": \"65\", \"resolution\": null}', 1, 9900),
(5, 'TV4', '{\"ports\": {\"p_usb\": \"2\", \"p_hdmi\": \"2\"}, \"p_screen\": \"42\", \"resolution\": null}', 1, 3300);
因为您的 update
方法没有 return 值,而是将查询放在 属性 中试试这个
$upMemList = [
'p_description' => "JSON_INSERT(`p_description` , '$.chipset' , 'Qualcomm')"
];
$dbh->update('products', $upMemList, 'id = :id');
$dbh->prepare($dbh->sql);
foreach ($upMemList as $fk => $fv) :
$dbh->bind(':' . $fk, $fv);
endforeach;
$dbh->bind('id', 1);
$dbh->execute(); // LINE 30
或者,由于查询已经是 class 的 属性,请将 prepare()
方法更改为使用 $this->sql
并准备
由于您的某些行已经包含 chipset
而其他行不包含,我认为您最好使用 JSON_SET()
在 JSON 文档和 [=25] 中插入或更新数据=]是结果。
$upMemList = [
'p_description' => "JSON_SET(`p_description` , '$.chipset' , 'Qualcomm')"
];
我有一个 class 可以从 table 中插入、更新和删除项目,所以当使用 JSON_INSERT 时 class 不能正常工作,它给出以下错误:
Fatal error: Uncaught PDOException: SQLSTATE[22032]: <>: 3140 Invalid JSON text: "Missing a name for object member." at position 1 in value for column 'products.p_description'. in /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php:182 Stack trace: #0 /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php(182): PDOStatement->execute() #1 /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/json_update.php(30): Database->execute() #2 {main} thrown in /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php on line 182
我在CRUD.php中使用的代码:
public function update($table, $fields = [], $where = null)
{
$args = [];
foreach ($fields as $fk => $fv) :
$fk = $this->strSafe($fk);
$args[] .= $fk . ' = :' . $fk;
endforeach;
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $args);
if ($where != null) {
$sql .= ' WHERE ' . $where;
}
$this->sql = $sql;
}
public function execute()
{
return $this->stmt->execute(); // LINE 182
}
要更新 table 字段“p_description”并添加值为“Qualcomm”的新项目“芯片组”,我使用了此代码 json_update.php:
$upMemList = [
'p_description' => "JSON_INSERT(`p_description` , '$.chipset' , 'Qualcomm')"
];
$upOn = $dbh->update('products', $upMemList, 'id = :id');
$dbh->prepare($upOn);
foreach ($upMemList as $fk => $fv) :
$dbh->bind(':' . $fk, $fv);
endforeach;
$dbh->bind('id', 1);
$dbh->execute(); // LINE 30
Table结构:
--
-- Table structure for table `products`
--
CREATE TABLE `products` (
`id` int UNSIGNED NOT NULL,
`p_name` varchar(50) DEFAULT NULL,
`p_description` json DEFAULT NULL,
`p_cat` int NOT NULL,
`p_price` int NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
--
-- Dumping data for table `products`
--
INSERT INTO `products` (`id`, `p_name`, `p_description`, `p_cat`, `p_price`) VALUES
(1, 'TV1', '{\"p_usb\": \"2\", \"p_hdmi\": \"3\", \"chipset\": \"Qualcomm\", \"p_screen\": \"21\", \"resolution\": null}', 1, 3400),
(2, 'TV2', '{\"p_usb\": \"3\", \"p_hdmi\": \"3\", \"p_screen\": \"43\", \"resolution\": null}', 1, 4500),
(3, 'Computer1', '{\"p_usb\": \"2\", \"p_hdmi\": \"0\", \"chipset\": \"Qualcomm\", \"p_screen\": \"15\", \"resolution\": null}', 4, 7350),
(4, 'TV3', '{\"ports\": {\"p_usb\": \"3\", \"p_hdmi\": \"2\"}, \"p_screen\": \"65\", \"resolution\": null}', 1, 9900),
(5, 'TV4', '{\"ports\": {\"p_usb\": \"2\", \"p_hdmi\": \"2\"}, \"p_screen\": \"42\", \"resolution\": null}', 1, 3300);
因为您的 update
方法没有 return 值,而是将查询放在 属性 中试试这个
$upMemList = [
'p_description' => "JSON_INSERT(`p_description` , '$.chipset' , 'Qualcomm')"
];
$dbh->update('products', $upMemList, 'id = :id');
$dbh->prepare($dbh->sql);
foreach ($upMemList as $fk => $fv) :
$dbh->bind(':' . $fk, $fv);
endforeach;
$dbh->bind('id', 1);
$dbh->execute(); // LINE 30
或者,由于查询已经是 class 的 属性,请将 prepare()
方法更改为使用 $this->sql
并准备
由于您的某些行已经包含 chipset
而其他行不包含,我认为您最好使用 JSON_SET()
在 JSON 文档和 [=25] 中插入或更新数据=]是结果。
$upMemList = [
'p_description' => "JSON_SET(`p_description` , '$.chipset' , 'Qualcomm')"
];