php - 查询 API 返回空响应
问题描述
大家好,我与 xampp 和 PHPMyAdmin 一起工作了几个月,以从本地主机上的 MySQL 数据库发送和获取数据,从那一周开始,当我发送请求时,我得到了一个空的正文响应,我不知道为什么我没有t 更改我的 PHP 代码中的任何内容,让我感到沮丧的是 Select 请求 API 无法正常工作,其他所有工作正常更新、插入......当我在 MySQL 数据库上运行相同的查询时,它工作正常我会很高兴从你们那里得到一些帮助。这是我在 index.php 中的代码
$db = new DbOperation();
$Bars = $db->Bars_Listing_name_adress();
if(count($Bars)<=0){
$response['error'] = true;
$response['message'] = 'Nothing found in the database';
}else{
$response['error'] = false;
$response['Bars'] = $Bars;
}
break;
这是 dboperations.php 中的代码
//get the list of the bars name and adresse for the api
public function Bars_Listing_name_adress(){
$stmt = $this->con->prepare("SELECT Bar_Name,Adress,Zip_code,City,Country FROM Bars");
$stmt->execute();
$stmt->bind_result($Bar_Name,$Adress,$Zip_code,$City,$Country );
$Bars = array();
while($stmt->fetch()){
$temp = array();
$temp['Bar_Name'] = $Bar_Name;
$temp['Adress'] = $Adress;
$temp['Zip_code'] = $Zip_code;
$temp['City'] = $City;
$temp['Country'] = $Country;
array_push($Bars, $temp);
}
return $Bars;
当我在 Postman 中调用它时
http://localhost/WebApi/v1/?op=Bars_Listing_name_adress
我得到一个空洞的回应,这很奇怪
POST
http://localhost/WebApi/v1/?op=Bars_Listing_name_adress&=
16:17:11.176
Pretty
Raw
Request Headers:
Content-Type:"application/x-www-form-urlencoded"
cache-control:"no-cache"
Postman-Token:"1093116d-58aa-4933-a571-99e42c0b203d"
User-Agent:"PostmanRuntime/7.6.0"
Accept:"*/*"
Host:"localhost"
accept-encoding:"gzip, deflate"
content-length:""
Response Headers:
Date:"Sat, 31 Oct 2020 15:17:11 GMT"
Server:"Apache/2.4.43 (Unix) OpenSSL/1.1.1g PHP/7.3.16 mod_perl/2.0.8-dev Perl/v5.16.3"
X-Powered-By:"PHP/7.3.16"
Content-Length:"0"
Keep-Alive:"timeout=5, max=100"
Connection:"Keep-Alive"
Content-Type:"text/html; charset=UTF-8"
Response Body:
但是当我在同一个 PHP 文件中调用任何其他请求时,它可以工作,所以问题只发生在没有 where 子句的 select 上,我很乐意从你们那里得到一些帮助,谢谢!
解决方案
感谢您的回答。我刚刚解决了这个问题。这是一个解码问题,我用以下方法解决了它utf8_encode
:
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
echo json_encode(utf8ize($response));
//echo json_encode($response);
推荐阅读
- python - argmax_cuda" 没有为 'Bool 实现
- r - 如何仅显示 geom_smooth 预测的一部分?
- tsql - 具有聚集索引的 SQL 顶级函数
- apache-spark - PySpark 将对象和库复制到所有工作节点 - Pandas UDF
- python - 如何在熊猫中合并交叉表的类别?
- android - 如何允许公众访问我的 android sdcard 上的应用程序数据文件夹
- python - 拖动窗口和释放窗口时的 Python 触发钩子
- vba - 依赖组合框
- c# - 来自 C# 中大型 CSV 文件的 WPF Datagrid
- serverless-framework - 通过 CircleCI 部署无服务器框架