我有两个PHP文件互相交谈。一个调用另一个函数来访问数据库并获取数据。
这是调用另一个的函数:
$app->post('/responsiveCalender', function() use ($app) {
verifyRequiredParams(array('restaurant_id'));
$restaurant_id = $app->request()->post('restaurant_id');
$db = new DbHandler();
$response = array();
$dish_details = $db->getRestaurantDishDetails($restaurant_id);
if($dish_details != null)
{
foreach($dish_details as $key)
{
$available_dates = $db->getAvailableDates($dish_details);
}
$response = $available_dates;
}
else {
$response['error'] = true;
$response['code'] = 102;
}
echoRespnse(200, $response);
});
如果error = 'true'
,则此代码会返回code = '102'
和$dish_details = null
。
这是调用的方法:
public function getRestaurantDishDetails($restaurant_id)
{
/* $result = mysqli_query($this->conn, "SELECT 'dish_id' FROM restaurant_dish WHERE restaurant_id = '$restaurant_id'") or die(mysqli_error($this->conn)); */
$stmt = $this->conn->prepare("SELECT 'avail_by_date' FROM `restaurant_dish` WHERE 'restaurant_id' = ? ");
$stmt->bind_param("i", $restaurant_id);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();
return $result;
}
我在REST客户端中传递了$restaurant_id = 22
,这在 PHPMyAdmin代码中为我提供了avail_by_date
。但它在我的 REST客户端中返回error='true'
。
当SQL语句不应为空时,有人能告诉我为什么这段代码返回一个空数组?
编辑:在所有代码上方添加 $result = array();
。
EDIT2:当前查询为:SELECT ``avail_by_date`` FROM ``restaurant_dish`` WHERE ``restaurant_id`` = 22
只有一对“不用担心。”
答案 0 :(得分:-1)
<?php
if ($stmt->bind_param("i", $restaurant_id) === false)
{
return array($stmt->error);
}
if ($stmt->execute() === false)
{
return array($stmt->error);
}