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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题