gpt4 book ai didi

PHP json编码数组并将返回值存储在mysql中

转载 作者:行者123 更新时间:2023-11-29 08:43:24 24 4
gpt4 key购买 nike

我想在 mysql 中将 php 数组存储为 JSON 。为此,我有 type='longtext' 的 meeting_point_json 列。

这是数组:

Array
(
[1] => Array
(
[date] => 23/4/2012
[meeting_time] => 23:04
[meeting_place] => town hall
[venue] => London
[opponents] => Tigers
[official_incharge] => Mr Putin
)

[2] => Array
(
[date] => 23/4/2050
[meeting_time] => 13:04
[meeting_place] => chief office
[venue] => Kenya
[opponents] => Peococks
[official_incharge] => Mr Black
)

[3] => Array
(
[date] => dsad
[meeting_time] => sadas
[meeting_place] => jjjjj
[venue] => jjjj
[opponents] => dasds
[official_incharge] => asad
)

)

这是 php 代码:

$data = json_encode($_POST['team_meeting_pt']);
$sql = "UPDATE yami_sub_team set meeting_point_json = $data where id = $subteam_id";
if(mysql_query($sql)){
exit("Done!");
}else{
die('Something went wrong, changes not saved. Error details: ' . mysql_error());
}

它应该可以工作,但相反,我收到一个错误:

Something went wrong, changes not saved. Error details: 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 '"1":{"date":"23\/4\/2012","meeting_time":"23:04","meeting_place":"town hall","ve' at line 1

知道我在这里做错了什么吗?

最佳答案

参见:$data 变量更改为 ".$data."$subteam_id 变量,带有 ".$subteam_id."

记住 sql 查询仅作为字符串发送,而不是内部变量......并按照您编写 PHP 的所有方式进行操作。始终用“..”和“..”包裹变量

“..”和“..”取决于您的 strip 包装器:例如:如果看起来像这样$string = mysql_query("从“.$variable.”中选择数据。”);所以,你必须使用“..”包装!如果像这样$string = mysql_query('从'.$variable.'中选择数据。'');因此,您必须使用 '..' 包装器!

祝 friend 好运

关于PHP json编码数组并将返回值存储在mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177050/

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