尝试从服务器获取数据时出现错误

时间:2020-04-26 22:46:04

标签: php android mysql pdo android-volley

当我尝试从服务器获取数据时遇到了一个问题。我用volley将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 []

1 个答案:

答案 0 :(得分:0)

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