php - 尝试从服务器获取数据时出错
问题描述
当我尝试从服务器获取数据时遇到了问题。我用凌空将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 []
解决方案
$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);
推荐阅读
- wordpress - 服务器重新启动时的临时占位符页面
- python - 如何在python中按照参考蒙版剪切图像的特定部分?
- html - 从非迭代 JSON 对象中获取值
- ios - iOS 14 Cordova Chrome 套接字绑定不起作用
- javascript - 在 Firestore 中更新后返回新更新的文档
- javascript - 如何等到 image.src 在 image.onload 事件函数中设置?
- amazon-s3 - 根据策略无效:策略条件失败:["eq"、"$x-amz-storage-class"、"STANDARD_IA"]
- c - 是否可以从内核空间使用 DRM 驱动程序?
- javascript - 卡住滑块脚本。如何正确设计滑块功能?
- python - Python中函数的这个特性正式称为什么?