我有2个php数组,我使用json_encode
和$mysqli->real_escape_string
将其编码为json。现在,json_encode之后这两个数组的输出是:
{\"arg1\":\"char*\",\"arg2\":\"char*\"}{\"arg1\":\"abc\",\"arg2\":\"bca\"}
然后,我用查询将其插入到mysql中。但我收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"arg1\":\"char*\",\"arg2\":\"char*\"}, {\"arg1\":\"abc\",\"arg2\":\"bca\"})' at line 1
我将这个json放入mysql的方式是什么?
Mysql查询
"UPDATE test_info SET java_auto_frame=$frame_auto, java_manual_frame=$frame_manual, java_testcase=$testcases, param_types=$param_types, param_examples=$param_examples WHERE qid=$qid"
其中,$ param_types是第一个json编码数组,$ param_examples是第二个。
答案 0 :(得分:2)
像这样编写你的查询
"UPDATE test_info SET java_auto_frame='$frame_auto', java_manual_frame='$frame_manual', java_testcase='$testcases', param_types='$param_types', param_examples='$param_examples' WHERE qid='$qid'"
答案 1 :(得分:1)
使用准备好的PDO声明(http://php.net/manual/en/book.pdo.php)。 像这样:
$dbh = new PDO("mysql:host=localhost;dbname=database;","root","");
$sql = "UPDATE test_info SET java_auto_frame=:frame_auto, java_manual_frame=:frame_manual, java_testcase=:testcases, param_types=:param_types, param_examples=:param_examples WHERE qid=:qid";
$sth = $dbh->prepare($sql);
$sth->execute(array(
'frame_auto' => $frame_auto,
'frame_manual' => $frame_manual,
'testcases' => $testcases,
'param_types' => $param_types,
'param_examples' => $param_examples,
'qid' => $qid
));