php - 使用 JSONArray 中的值的 Mysql 查询

标签 php mysql post

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

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

最佳答案

如果我们有一个 JSON 数组的示例会有所帮助,但是,您应该将 JSON 数组解码为逗号分隔的列表以与 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 进行新开发:

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%.

相关阅读:

关于php - 使用 JSONArray 中的值的 Mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13727550/

相关文章:

Angular 2 Http Post 将 Content-Length header 设置为 0

.net - 如何在 ASP.NET MVC 3 中发布文件数组?

php - Codeigniter - 面临创建多个缩略图的问题

php - 将输出存储在 mysql 中

php - 如何找出用户上传文件的字符集?

php - 从表中获取下拉列表的内容并将其存储在数组中

php - 当用户单击 PHP 中的提交按钮时,如何连接到数据库、拍摄和发送电子邮件以及更新数据库?

php - Magento 数据库中是否有一个字段保存订单发货的日期?

php - ISO 3166-1 alpha-2 国家/地区代码的区域名称

ruby-on-rails - 目录 : How to authenticate user before POST