gpt4 book ai didi

php - 使用 mysql 查询(codeigniter)检索以 json 格式保存的数据

转载 作者:行者123 更新时间:2023-11-29 10:56:57 25 4
gpt4 key购买 nike

M 使用 json 格式将数据保存到数据库中,例如示例:

state_id city_id
["21"] ["32,35,67"]

现在想要使用 mysql 查询(codeigniter)进行搜索其中 state_id = 21 AND city_id = 32

任何人都可以帮助我吗,在谷歌搜索时我发现了一个东西“json_extract”,但它不知道如何在查询中使用它。

最佳答案

如果我理解正确的话,这就是答案:

$json = '{"state_id": 21, "city_id": [32,35,67]}';
$decodedJson = json_decode($json, true);

/** @var $state_id int */
/** @var $city_id array */
extract($decodedJson);

$sql = printf("SELECT * FROM `table_name` WHERE `state_id` = %d AND `city_id` IN ( %s );", $state_id, join(',', $city_id));

echo $sql.PHP_EOL;

输出:

SELECT * FROM table_name WHERE state_id = 1 AND city_id IN ( 1,2,3 );

关于php - 使用 mysql 查询(codeigniter)检索以 json 格式保存的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886218/

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