dsshsta97935 2016-12-29 10:26
浏览 93
已采纳

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

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

$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 :

$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 ?

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条回答 默认 最新

  • dongwu9063 2016-12-29 15:09
    关注

    Can you please try this

    $sql = "select * from posts ORDER BY post_id DESC LIMIT '".$tag."';";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料