使用 JSONArray 中的值进行 Mysql 查询 [英] Mysql query using values in JSONArray

查看:145
本文介绍了使用 JSONArray 中的值进行 Mysql 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个 JSONArray.toString() 发布到一个 php 文件中.JSONArray 包含一组 id 值.我想做一个 mysql 查询,它只返回包含在 jsonarray 中的 id 的行.我下面的方法不起作用.还有别的方法吗?

I'm posting a JSONArray.toString() to a php file. The JSONArray contains a set of id values. I want to make a mysql query that only returns rows that have an id contained in the jsonarray. My method below isn't working. Is there another way?

$jsonarray= $_POST["ids"];
$query = mysql_query("SELECT * FROM table WHERE id IN $jsonarray")or die(mysql_error());

推荐答案

如果我们有 JSON 数组的示例会有所帮助,但是,您应该将 JSON 数组解码为逗号分隔的列表以与 IN 一起使用.

It'd help if we had an example of the JSON array, however, you should decode the JSON array into a comma separated list to use with IN.

$jsonarray = $_POST["ids"];
$ids = implode(",", json_decode($jsonarray,true));

$query = mysql_query("SELECT * FROM table WHERE id IN ($ids)")or die(mysql_error());


此外,您应该使用 mysqli 或 PDO_MySQL 进行新的开发:


Also, you should be using mysqli or PDO_MySQL for new development:

建议使用 mysqli 或 PDO_MySQL 扩展.它不建议使用旧的mysql扩展进行新开发.下面提供了详细的特征比较矩阵.整体所有三个扩展的性能被认为是关于相同的.尽管扩展的性能仅有助于PHP Web 请求总运行时间的一部分.通常,影响低至 0.1%.

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

相关阅读:

这篇关于使用 JSONArray 中的值进行 Mysql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆