我正在使用Slim Framework处理我的RESTful Web服务,当我尝试从3个表中获取数据时出现此错误。
这是我班上的功能:
public function getCard($card_id, $card_type_id, $user_id) {
$stmt = $this->conn->prepare("SELECT uc.id, c.card_name, ct.category, c.card_desc, c.card_picture ,c.status, c.created_at from cards c, user_cards uc, cards_type ct WHERE c.card_type_id = ct.card_type_id AND c.card_id = uc.card_id AND uc.user_id = ?");
if ($stmt == FALSE) {
die($this->conn->error);
} else {
$stmt->bind_param("iii", $card_id, $card_type_id, $user_id);
if ($stmt->execute()) {
$res = array();
$stmt->bind_result($id, $card_name, $category, $card_desc, $card_picture, $status, $created_at);
$stmt->fetch();
$res["id"] = $id;
$res["card_name"] = $card_name;
$res["category"] = $category;
$res["card_desc"] = $card_desc;
$res["card_picture"] = $card_picture;
$res["status"] = $status;
$res["created_at"] = $created_at;
$stmt->close();
return $res;
} else {
return NULL;
}
}
}
这是index.php中的代码:
$app->get('/cards/users/:card_id', 'authenticate', function($card_id, $card_type_id) {
global $user_id;
$response = array();
$db = new Card();
// fetch card
$result = $db->getCard($card_id, $card_type_id, $user_id);
if ($result != NULL) {
$response["error"] = false;
$response["id"] = $result["id"];
$response["card_name"] = $result["card_name"];
$response["category"] = $result["category"];
$response["card_desc"] = $result["card_desc"];
$response["card_picture"] = $result["card_picture"];
$response["status"] = $result["status"];
$response["createdAt"] = $result["created_at"];
echoRespnse(200, $response);
} else {
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
echoRespnse(404, $response);
}
});
我写错了吗?
答案 0 :(得分:0)
查看此行:
$stmt = $this->conn->prepare("SELECT uc.id, c.card_name, ct.category, c.card_desc, c.card_picture ,c.status, c.created_at from cards c, user_cards uc, cards_type ct WHERE c.card_type_id = ct.card_type_id AND c.card_id = uc.card_id AND uc.user_id = ?");
查询可能很难看到,因此我只会格式化查询:
SELECT uc.id,
c.card_name,
ct.category,
c.card_desc,
c.card_picture,
c.status,
c.created_at
FROM cards c,
user_cards uc,
cards_type ct
WHERE c.card_type_id = ct.card_type_id
AND c.card_id = uc.card_id
AND uc.user_id = ?
请注意,它只有一个问号,表示您只应将一个参数绑定到预备语句。
所以你应该改变这一行:
$stmt->bind_param("i", $user_id); //have only one argument