如何管理将Jsonobject的int值放入查询?

时间:2021-04-30 19:37:42

The app send a String/Jsonobject with POST method to a PHP file. And I want to use that value to set the Limit in the query

该应用程序使用POST方法将String / Jsonobject发送到PHP文件。我想使用该值在查询中设置限制

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

I can receive it but i can't use it into my query. I got errors like "java.lang.string cannot be converted to jsonarray" I tried to convert it into Integer :

我可以收到它,但我不能将它用于我的查询。我得到的错误像“java.lang.string无法转换为jsonarray”我试图将其转换为Integer:

$int = intval($tag);

but the query still doesn't see it as an Integer..

但查询仍然没有将其视为整数..

There is another way to get an Int from Activity.java to php so that i can put it in the query after ORDER BY post_id DESC LIMIT ?

还有另一种方法可以将一个Int从Activity.java转换为php,以便我可以在ORDER BY post_id DESC LIMIT之后将它放入查询中吗?

Java

Java的

 RequestQueue queue = Volley.newRequestQueue(this);
    Map<String, String> params = new HashMap<>();
    params.put("tag", "2");
    JSONObject o = new JSONObject(params);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
            Request.Method.POST,

Php

<?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));

1 个解决方案

#1


0  

Can you please try this

你能试试吗?

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

#1


0  

Can you please try this

你能试试吗?

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