Php foreach代码不适用于筛选API
按城市和酒店搜索我的数据库中的城市和酒店。我的代码没有工作。你能查一下我的密码吗。也许代码中有错误Php foreach代码不适用于筛选API,php,sql,wordpress,api,Php,Sql,Wordpress,Api,按城市和酒店搜索我的数据库中的城市和酒店。我的代码没有工作。你能查一下我的密码吗。也许代码中有错误 $countryName = $_GET['countrycode']; $url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&
$countryName = $_GET['countrycode'];
$url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];
$finalArray = [];
$dbResults = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM hotels
WHERE county = %s",
$countryName
),
ARRAY_A
);
# If atleast 1 row is fecthed from
# database
if ($dbResults->num_rows) {
foreach ($dbResults as $dbResult) {
echo $storedCountryName = $dbResult["county"];
echo $storedHotelName = $dbResult["hotel"];
foreach ($apiResults as $apiResult) {
if ($storedCountryName === $apiResult['address']['city']
&& $storedHotelName === $apiResult['property_name'])
{
$finalArray[] = $apiResult;
}
}
}
}
var_dump($finalArray);
var_dump($finalArray);结果:
array (size=0)
empty
试试这个,让我知道 我将这一行
if($dbResults->num_rows){
替换为if($wpdb->num_rows>0){
它应该会起作用
$countryName = $_GET['countrycode'];
$url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];
$finalArray = [];
$dbResults = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM hotels
WHERE county = %s",
$countryName
),
ARRAY_A
);
# If atleast 1 row is fecthed from
# database
if ($wpdb->num_rows>0) {
foreach ($dbResults as $dbResult) {
echo $storedCountryName = $dbResult["county"];
echo $storedHotelName = $dbResult["hotel"];
foreach ($apiResults as $apiResult) {
$ResultArray= array();
if ($storedCountryName == $apiResult['address']['city']
&& $storedHotelName == $apiResult['property_name'])
{
$ResultArray = $apiResult;
$finalArray[] = $ResultArray;
}
}
}
}
var_dump($finalArray);
试试这个,让我知道 我将这一行
if($dbResults->num_rows){
替换为if($wpdb->num_rows>0){
它应该会起作用
$countryName = $_GET['countrycode'];
$url = 'https://api.sandbox.amadeus.com/v1.2/hotels/search-box?apikey=EbdwHszYlp0f7fQB6YHo40SIhu4ZAomm&south_west_corner=51.328476%2C%20-10.758522&north_east_corner=55.320949%2C%20-5.474099&check_in=2017-06-19&check_out=2017-06-29&number_of_results=1000';
$response = file_get_contents($url);
$apiResults = json_decode($response, true);
$apiResults = $apiResults["results"];
$finalArray = [];
$dbResults = $wpdb->get_results(
$wpdb->prepare("
SELECT *
FROM hotels
WHERE county = %s",
$countryName
),
ARRAY_A
);
# If atleast 1 row is fecthed from
# database
if ($wpdb->num_rows>0) {
foreach ($dbResults as $dbResult) {
echo $storedCountryName = $dbResult["county"];
echo $storedHotelName = $dbResult["hotel"];
foreach ($apiResults as $apiResult) {
$ResultArray= array();
if ($storedCountryName == $apiResult['address']['city']
&& $storedHotelName == $apiResult['property_name'])
{
$ResultArray = $apiResult;
$finalArray[] = $ResultArray;
}
}
}
}
var_dump($finalArray);
试一试
=
表示您也在检查数据类型。可能您的数据库字段数据类型和json响应字符串数据类型不匹配。因此从条件中删除一个=并仅比较值。尝试
=
表示您也在检查数据类型。可能您的数据库字段数据类型和json响应字符串数据类型不匹配。因此从条件中删除一个=并仅比较值。从county=%s的酒店选择*的结果是什么?Hi@vel,它的长代码你能从这里下载从county=%s的酒店选择*的结果吗?嗨@vel,它的长代码。你能从这里下载吗?我用如果($dbResults->num_rows){
替换这一行如果($wpdb->num_rows>0){
它工作了,但只显示一个酒店信息!在api url中,两个酒店与我的匹配database@rayan005,回答已更新。立即检查。仍然相同的概率回答已再次更新。您必须检查if条件。打印这些值并检查$storedCountryName
&&$storedHotelName
。如果($dbResults->num_rows){withif($wpdb->num_rows>0){
它工作了,但只显示一个酒店信息!在api url中,两个酒店与我的匹配database@rayan005,答案更新。现在检查。仍然相同的概率答案再次更新。您必须检查if条件。打印这些值并检查$storedCountryName
&&$storedHotelName
。我可以知道为什么得到-1吗?我不认为我的答案不正确。@downvoter愿意留下解释:)我可以知道为什么我得到-1吗?我不认为我的答案不正确。@downvoter愿意留下解释:)