首页 > 解决方案 > 尝试从服务器获取数据时出错

问题描述

当我尝试从服务器获取数据时遇到了问题。我用凌空将arraylist数据发送到php服务器。

ArrayList<String> list = new ArrayList<>();

list.add("data1");
list.add("data2");
list.add("data3");

String finalRequestedList = new Gson().toJson(list);

我将上面的 json 正确发送到服务器并在 php 上接收到它。但我无法运行查询并从我的 android 上的服务器获取响应。

下面是我的 php 代码:

$data =$_POST['data'];
$content = json_decode($data,true); 
$in  = str_repeat('?,', count($content) - 1) . '?';
$query = "SELECT * from table where column = '$in'";
$result=$connection->prepare($query);
        $result->execute($content);
        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
        print json_encode($rows);

我的目标是运行这些查询并从数据库中获取响应并将其返回到 android。

$query = "SELECT * from table where column = 'data1,data2,data3'";

有人可以帮我处理这个案子吗?在此先感谢。

编辑:凌空将此json发送到服务器:

["data1","data2","data3"]

我在 php 上遇到了这个错误(行不同,因为我有一些与这部分无关的其他代码):

Warning: count(): Parameter must be an array or an object that implements 
Countable in domain.com/get_data.php on line 76 Warning: str_repeat(): Second 
argument has to be greater than or equal to 0 in domain.com/get_data.php on 
line 76 []

标签: phpandroidmysqlpdoandroid-volley

解决方案


$data =$_POST['data'];
$content = json_decode($data,true); 
$in=str_repeat("? or column=",count($content)-1)."?";
$query = "SELECT * from table where (column=$in)";
$result=$connection->prepare($query);
$result->execute($content);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
print json_encode($rows);

推荐阅读