【问题标题】:mysql where from an array jsonmysql 从哪里来的数组 json
【发布时间】:2017-03-15 06:10:28
【问题描述】:

我对这个问题感到厌烦,我有一个数组,但是是 json 格式,我从 select2 多个值 ex (name=place[]) 中得到它, 但它是这样的:

Array ( [0] => Hematology Rutin [1] => Eritrosit )

使用 print_r。

这是问题。 我如何从名称 IN ('".$array."') 的产品中选择 * .

我怎样才能将该数组展开为像这样的简单 php 数组

$array = "Hematology,Eritrosit"

我尝试echo json_encode($lab);,但轮到这样["Hematology Rutin","Eritrosit"] 我希望有人可以帮助我..谢谢

【问题讨论】:

  • 符号是IN (?,?),其中? 是您绑定数据的占位符。您不能随意将其编码为 JSON 并期望它能够正常工作。如果您想看的话,文档会以极其精确的方式列出它,我鼓励您这样做。

标签: php mysql arrays json


【解决方案1】:

将 json 转换为数组。然后根据需要将数组转换为字符串并在查询中使用。

如下所示:

$array = json_decode($json, TRUE);   // convert json to array
$list = implode ('\',\'', $array);  // Convert array to string

在 SQL 查询中使用

$sql = "select * from product where name IN ('" . $list . "')";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2018-10-05
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2019-12-06
    相关资源
    最近更新 更多