我正在使用 Zend Framework (1.12),我想创建一个基于 JSON 文件的表。我已经创建了表及其字段(现在它们都是长文本),它所要做的就是将它们插入到正确的列中。我遵循了这些例子:
http://www.daniweb.com/web-development/php/threads/381669/json-to-mysql-with-php http://www.daniweb.com/web-development/php/threads/381669/json-to-mysql-with-php(第二篇文章)将 JSON 解析为 mySQL https://stackoverflow.com/questions/11257694/parse-json-to-mysql
问题是我的 JSON 的构造方式不同(我的 JSON 有一个名为 Actie 的“根元素”,不知道正确的术语),其中包含一个包含所有对象的数组。目前,我正在使用这段代码:
$actieurl = "http://creative3s.com/thomas/nmdad/actie.json";
$my_arr = json_decode(file_get_contents($actieurl));
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => 'localhost',
'username' => 'root',
'password' => NULL,
'dbname' => 'zf-tutorial'
));
foreach($my_arr as $key => $value){
$sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'";
}
$sqlclause = implode(",",$sql);
$query = "INSERT INTO `testerdetest` SET $sqlclause";
$db->query($query);
但我收到一条错误消息,说我正在传递一个数组:
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Users\Thomas\Documents\GitHub\NMDAD-testing\application\controllers\IndexController.php on line 29
有谁知道如何用这种格式的 JSON 解决这个问题?请记住,我无法以任何方式更改 JSON。额外链接:
JSON: http://creative3s.com/thomas/nmdad/actie.json http://creative3s.com/thomas/nmdad/actie.json表结构:https://i.stack.imgur.com/HPKVL.png https://i.stack.imgur.com/HPKVL.png