我正在将 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/