添加分页后,PHP函数不再显示mysql数组数据
我制作了一个PHP函数,从数据库中的表(product_reviews)中提取数据,显示唯一product_id的所有评论。以下是代码:添加分页后,PHP函数不再显示mysql数组数据,php,mysql,arrays,pagination,Php,Mysql,Arrays,Pagination,我制作了一个PHP函数,从数据库中的表(product_reviews)中提取数据,显示唯一product_id的所有评论。以下是代码: function showReviews ($product_id) { include('database_conn.php'); $query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."'"; if ($que
function showReviews ($product_id)
{
include('database_conn.php');
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
}
功能showReviews($product\u id)
{
包括('database_conn.php');
$query=“从product_reviews中选择*,其中product_reviews.productID=”“$product_id.””;
if($queryresult=mysqli\u query($conn,$query))
{
而($currentrow=mysqli\u fetch\u assoc($queryresult))
{
$result_list[]=$currentrow;
}
foreach($result\u列表为$currentrow)
{
$productitems[]=数组(
$customer\u forename=$currentrow['customer\u forename'],
$customer\u LANSAME=$currentrow['customer\u LANSAME'],
$review\u title=$currentrow['review\u title'],
$review_text=$currentrow['review_text']);
回音'
但是当我向函数添加分页时(遵循ehow教程),函数没有任何输出()
如果(!function_存在('showReviews'))
{
功能展示评论($product\U id)
{
包括('database_conn.php');
包括('functions.php');
$rowsPerPage=3;
$currentPage=(isset($\u GET['page'])和&$\u GET['page']>0)?(int)$\u GET['page']:1);
$offset=($currentPage-1)*$rowsPerPage;
$query=“从product_reviews中选择*,其中product_reviews.productID=”“$product_id。”“LIMIT”“。$offset”“,“$rowsPerPage”“”;
if($queryresult=mysqli\u query($conn,$query))
{
而($currentrow=mysqli\u fetch\u assoc($queryresult))
{
$result_list[]=$currentrow;
}
foreach($result\u列表为$currentrow)
{
$productitems[]=数组(
$customer\u forename=$currentrow['customer\u forename'],
$customer\u LANSAME=$currentrow['customer\u LANSAME'],
$review\u title=$currentrow['review\u title'],
$review_text=$currentrow['review_text']);
回声';
}
如果($currentPage<$totalPages)
{
回声';
}
}
}
我测试了我的sql查询,它在mysql工作台中运行良好。
我做错了什么?有人能推荐一种更好的方法吗?当您创建查询时,您正在封装单引号之间的偏移量和限制(如果我正确阅读了您的代码)
从product\u reviews中选择*,其中product\u reviews.productID='25'限制为'0','3'
尝试删除这些单引号。如果page=1,您的限制将为0。它不会显示任何内容。您用于访问第一页和第二页的URL是什么?@RajdeepPaul您是指评论的第一页和第二页吗?它们是SingleProductPage.php?id=1&page=1和SingleProductPage.php?id=1&page=2。@YuriTkachenko我也这么认为l、 但是当我运行查询“SELECT*FROM product_reviews WHERE product_reviews.productID='1'LIMIT 0,3;”在Mysql Workbench中,它给出了表中的前3行。您在Mysql Workbench上尝试查询时是否使用了相同的数据库?如果您可以访问db服务器,调试查询日志可能是一个好主意:与原始问题无关,我建议使用PDO而不是mysqli,组织查询也是一个好主意ur代码,以便标记(模板)与逻辑分离。
if(!function_exists('showReviews'))
{
function showReviews ($product_id)
{
include('database_conn.php');
include('functions.php');
$rowsPerPage = 3;
$currentPage = ((isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1);
$offset = ($currentPage-1)*$rowsPerPage;
$query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."' LIMIT '".$offset."','".$rowsPerPage."'";
if ($queryresult = mysqli_query($conn, $query))
{
while ($currentrow = mysqli_fetch_assoc($queryresult))
{
$result_list[] = $currentrow;
}
foreach ($result_list as $currentrow)
{
$productitems[] = array(
$customer_forename = $currentrow['customer_forename'],
$customer_surname = $currentrow['customer_surname'],
$review_title = $currentrow['review_title'],
$review_text = $currentrow['review_text']);
echo '<article class="Review_Box">';
echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
echo "<p>".$review_text."</p>";
echo "</article>";
}
}
$count = countReviews (1);
$totalPages = $count/$rowsPerPage;
if($currentPage > 1)
{
echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage-1) . '#Customer_Reviews">Previous Page</a> ';
}
if($currentPage < $totalPages)
{
echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage+1) . '#Customer_Reviews">Next Page</a>';
}
}
}