Php 如何将Jsonobject中的int值放入查询?

Php 如何将Jsonobject中的int值放入查询?,php,android,json,Php,Android,Json,应用程序使用POST方法将字符串/Jsonobject发送到PHP文件。 我想用这个值来设置查询中的限制 $sql = "select * from posts ORDER BY post_id DESC LIMIT $tag"; 我可以接收它,但无法将其用于查询。 我遇到了类似“java.lang.string无法转换为jsonarray”的错误 我尝试将其转换为整数: $int = intval($tag); 但是查询仍然没有将其视为整数 还有另一种方法可以从Activity.java到

应用程序使用POST方法将字符串/Jsonobject发送到PHP文件。 我想用这个值来设置查询中的限制

$sql = "select * from posts ORDER BY post_id DESC LIMIT $tag";
我可以接收它,但无法将其用于查询。 我遇到了类似“java.lang.string无法转换为jsonarray”的错误 我尝试将其转换为整数:

$int = intval($tag);
但是查询仍然没有将其视为整数

还有另一种方法可以从Activity.java到php获取Int,这样我就可以在ORDER BY post_id DESC LIMIT之后将其放入查询中

爪哇

RequestQueue queue=Volley.newRequestQueue(this);
Map params=新的HashMap();
参数put(“标签”、“2”);
JSONObject o=新的JSONObject(参数);
JsonObjectRequest JsonObjectRequest=新JsonObjectRequest(
Request.Method.POST,
Php


你能试试这个吗

$sql = "select * from posts ORDER BY post_id DESC LIMIT '".$tag."';";

$tag
不是
&tag
。这里只是一个输入错误^^Thx作为回复,我得到了JSONExecption:java.lang.String类型的值错误无法转换为JSONArray。我试图$myfile=fopen(“newfile.txt”,“w”)或死(“无法打开文件!”;fwrite($myfile,$tag);fclose($myfile);然后我得到文件中的值…例如6..你认为我需要将其转换为另一种类型吗?就像我说的,即使intval也可以转换,但查询仍然不将其视为INT或甚至string如果你只需要发送INT参数,我建议,避免
JSONRequest
,只需像这样使用
get
方法
>这就是我解决问题的方法!谢谢芦山!
<?php

    //open connection to mysql db
        $connection = mysqli_connect("xxxxx","xxxx","xxxx","xxxx")
 or die("Error " . mysqli_error($connection));

    $data = json_decode(file_get_contents('php://input'), true);

    if (empty($data['tag']) == false) {
       $tag = $data['tag'];
    }

            //fetch table rows from mysql db 
        $sql = "select * from posts ORDER BY post_id DESC LIMIT $tag";

        $result = mysqli_query($connection, $sql) 
or die("Error in Selecting " . mysqli_error($connection));
$sql = "select * from posts ORDER BY post_id DESC LIMIT '".$tag."';";