【发布时间】:2020-10-31 15:22:09
【问题描述】:
大家好,我已经使用 xampp 和 PHPMyAdmin 工作了几个月,以从本地主机上的 MySQL 数据库发送和获取数据,并且从那周开始,当我发送请求时,我得到了一个空的正文响应 我不知道为什么我没有更改我的 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 上 我很乐意从你们那里得到一些帮助 thx !!
【问题讨论】:
-
您是否在代码中的任何位置回显结果?
-
从字面上输入与@LawrenceCherone 相同的内容!你也说它确实有效,所以有什么改变吗?是否有一些数据日期绑定,您在测试
DELETE请求时是否不小心删除了一些内容?您是否查看了日志/打开了错误以确保后台没有任何问题? -
如果您没有测试,调试 API 可能会有点麻烦。理想情况下,编写一些测试将是一个非常好的起点(感谢新开发者不知道/开始学习测试,直到更远的地方,这在某些方面确实是一种耻辱)。如果您没有测试,请确保您可以通过运行
var_dump()然后die()实际到达终点。还可以尝试在浏览器而不是邮递员中点击端点,以确保这不是邮递员问题