因此,在从数据库中选择一些列和行后,我必须将它们放入数组中,将它们放入数组中,我使用foreach循环,并且它有效。然后,当它循环查询结果时,我需要执行一些查询,这些查询基于查询结果的值,循环仍在工作。但我得到了错误。这是我的代码。你能帮我解决这个问题吗?我是Php的新手所以我不知道要解决它。
<?php
require("config.php");
$whatvenue= $_POST['whatvenue'];
$showby= $_POST['showedby'];
if($whatvenue==="hotel")
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
else if($whatvenue==="restoran")
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
else
{
$query = "Select * FROM venues where venue_category like :whatvenue";
}
$query_params = array(
':whatvenue' => $_POST['whatvenue']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$queryreviews ="SELECT COUNT( VENUE_ID) AS TOTALREVIEWER FROM COMMENT WHERE VENUE_ID =:venue_id";
$queryrsb ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERSB FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =5";
$queryrb ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERB FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =4";
$queryrc ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERC FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =3";
$queryrk ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERK FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =2";
$queryrsk ="SELECT COUNT( USER_ID ) AS TOTALREVIEWERSK FROM COMMENT WHERE VENUE_ID =:venue_id AND REVIEW_RATE =1";
$rows = $stmt->fetchAll();
if ($rows)
{
$response["success"] = 1;
$response["message"] = "Venue Ditampilkan!";
$response["venues"] = array();
foreach ($rows as $row) {
$venues = array();
$venues["venue_id"] = $row["VENUE_ID"];
$query_paramsr = array(
':venue_id' => $venues["venue_id"]
);
try
{
$stmtr = $db->prepare($queryreviews);
$resultr = $stmtr->execute($query_paramsr);
$stmtrsb = $db->prepare($queryrsb);
$resultsb = $stmtrsb->execute($query_paramsr);
$stmtrb = $db->prepare($queryrb);
$resultrb = $stmtrb->execute($query_paramsr);
$stmtrc = $db->prepare($queryrc);
$resultrc = $stmtrc->execute($query_paramsr);
$stmtrk = $db->prepare($queryrk);
$resultrk = $stmtrk->execute($query_paramsr);
$stmtrsk = $db->prepare($queryrsk);
$resultrsk = $stmtrsk->execute($query_paramsr);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rowsr = $stmtr ->fetch();
$rowsrsb = $stmtrsb->fetch();
$rowsrb = $stmtrb->fetch();
$rowsrc = $stmtrc->fetch();
$rowsrk = $stmtrk->fetch();
$rowsrsk = $stmtrsk->fetch();
$venues["venue_totalreviwer"] = $rowsr ["TOTALREVIEWER"];
$venues["venue_totalreviwersb"] = $rowsrsb["TOTALREVIEWERSB"];
$venues["venue_totalreviwerb"] = $rowsrb ["TOTALREVIEWERB"];
$venues["venue_totalreviwerc"] = $rowsrc["TOTALREVIEWERC"];
$venues["venue_totalreviwerk"] = $rowsrk["TOTALREVIEWERK"];
$venues["venue_totalreviwersk"] = $rowsrsk["TOTALREVIEWERSK"];
$venues["venue_name"] = $row["VENUE_NAME"];
$venues["venue_address"] = $row["VENUE_ADDRESS"];
$venues["venue_desc"] = $row["VENUE_DESC"];
$venues["venue_lat"] = $row["VENUE_LAT"];
$venues["venue_long"] = $row["VENUE_LONG"];
$venues["venue_category"] = $row["VENUE_CATEGORY"];
$venues["venue_phone"] = $row["VENUE_PHONE"];
$venues["venue_email"] = $row["VENUE_EMAIL"];
$venues["venue_website"] = $row["VENUE_WEBSITE"];
$venues["venue_picture"] = $row["VENUE_PICTURE"];
$venues["venue_rating"] = $row["VENUE_RATING"];
//update our repsonse JSON data
array_push($response["venues"], $venues);
}
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "Venue yang diminta tidak tersedia";
die(json_encode($response));
}
?>
答案 0 :(得分:0)
如果您可以一次性获取所需的所有信息,那么多次点击数据库并不是一个好主意。基本查询的可能版本,其完全可能是
SELECT v.venue_id
,q.totalreviewersb
,q.totalreviewerb
,q.totalreviewerc
,q.totalreviewerk
,q.totalreviewersk
,v.venue_name
,v.venue_address
,v.venue_desc
,v.venue_lat
,v.venue_long
,v.venue_category
,v.venue_phone
,v.venue_email
,v.venue_website
,v.venue_picture
,v.venue_rating
FROM venus v JOIN
(
SELECT v.venue_id
,COUNT(*) totalreviewer
,SUM(CASE WHEN c.review_rate = 5 THEN 1 ELSE 0 END) totalreviewersb
,SUM(CASE WHEN c.review_rate = 4 THEN 1 ELSE 0 END) totalreviewerb
,SUM(CASE WHEN c.review_rate = 3 THEN 1 ELSE 0 END) totalreviewerc
,SUM(CASE WHEN c.review_rate = 2 THEN 1 ELSE 0 END) totalreviewerk
,SUM(CASE WHEN c.review_rate = 1 THEN 1 ELSE 0 END) totalreviewersk
FROM venus v LEFT JOIN comment c
ON v.venue_id = c.venue_id
WHERE v.venue_category LIKE :whatvenue
GROUP BY v.venue_id
) q ON v.venue_id = q.venue_id
现在你的php代码可能会被归结为类似的东西
<?php
require('config.php');
$whatvenue= $_POST['whatvenue'];
//$showby= $_POST['showedby'];
$query = 'SELECT
v.venue_id
,q.totalreviewersb
,q.totalreviewerb
,q.totalreviewerc
,q.totalreviewerk
,q.totalreviewersk
,v.venue_name
,v.venue_address
,v.venue_desc
,v.venue_lat
,v.venue_long
,v.venue_category
,v.venue_phone
,v.venue_email
,v.venue_website
,v.venue_picture
,v.venue_rating
FROM venus v JOIN
(
SELECT v.venue_id
,COUNT(*) totalreviewer
,SUM(CASE WHEN c.review_rate = 5 THEN 1 ELSE 0 END) totalreviewersb
,SUM(CASE WHEN c.review_rate = 4 THEN 1 ELSE 0 END) totalreviewerb
,SUM(CASE WHEN c.review_rate = 3 THEN 1 ELSE 0 END) totalreviewerc
,SUM(CASE WHEN c.review_rate = 2 THEN 1 ELSE 0 END) totalreviewerk
,SUM(CASE WHEN c.review_rate = 1 THEN 1 ELSE 0 END) totalreviewersk
FROM venus v LEFT JOIN comment c
ON v.venue_id = c.venue_id
WHERE v.venue_category LIKE :whatvenue
GROUP BY v.venue_id
) q ON v.venue_id = q.venue_id';
try {
$stmt = $db->prepare($query);
$stmt->execute(array(':whatvenue' => $whatvenue));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $ex) {
$response['success'] = 0;
$response['message'] = 'Database Error!';
die(json_encode($response));
}
$response['success'] = 1;
$response['message'] = 'Venue Ditampilkan!';
$response['venues' ] = rows();
// echoing JSON response
echo json_encode($response);
注意:显然,提供的代码示例尚未经过测试