gpt4 book ai didi

php - $stmt->fetch() 未获取数据

转载 作者:行者123 更新时间:2023-11-29 07:09:19 28 4
gpt4 key购买 nike

我不明白为什么我的函数没有得到结果而只是得到空值。我正在使用 PhpStorm;我的所有连接都很好(Apache、MySQL、PhpMyAdmin),我已经检查过它们,其他其他服务也正常工作。

这是我的dbhandler.php 文件:

public function getRating($rating_id, $user_id) {
$stmt = $this->conn->prepare("SELECT ur.restaurant_id, ur.service_rating, ur.food_rating, ur.music_rating FROM user_ratings ur , user u WHERE u.user_id = ? AND u.user_id = ur.user_id AND ur.rating_id = ?");
$stmt->bind_param("ii", $rating_id, $user_id);


if ($stmt->execute()) {

$stmt->bind_result( $restaurant_id, $service_rating, $food_rating, $music_rating);
// TODO
//$rating_id = $stmt->get_result()->fetch_assoc();
$stmt->fetch();
$res = array();
$res["user_id"] = $user_id;
$res["rating_id"] = $rating_id;
$res["restaurant_id"] = $restaurant_id;
$res["service_rating"] = $service_rating;
$res["food_rating"] = $food_rating;
$res["music_rating"] = $music_rating;
$stmt->close();

return $res;
} else {
return NULL;
}
}

这是我的 index.php 文件

$app->get('/userRatings/:rating_id', 'authenticate', function($rating_id) {
global $user_id;
$response = array();
$db = new DbHandler();

// fetch rating
$result = $db->getRating($rating_id, $user_id);

if ($result != NULL) {
$response["user_id"] = $result["user_id"];
$response["rating_id"] = $result["rating_id"];
$response["restaurant_id"] = $result["restaurant_id"];
$response["service_rating"] = $result["service_rating"];
$response["food_rating"] = $result["food_rating"];
$response["music_rating"] = $result["music_rating"];
echoRespnse(200, $response);
} else {
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
echoRespnse(404, $response);
}
});

请求的响应是:

{"user_id":19,
"rating_id":"171",
"restaurant_id":null,
"service_rating":null,
"food_rating":null,
"music_rating":null}

最佳答案

参数值有问题。按如下方式更改参数值。

$stmt = $this->conn->prepare("SELECT  ur.restaurant_id, ur.service_rating, ur.food_rating, ur.music_rating FROM user_ratings ur , user u WHERE u.user_id = ? AND u.user_id = ur.user_id  AND ur.rating_id = ?");

$stmt->bind_param("ii", $user_id,$rating_id);

根据你的sql第一个参数应该是$user_id。不是 $ rating_id。

根据您的参数设置,没有要获取的记录。

关于php - $stmt->fetch() 未获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40405221/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com