添加分页后,PHP函数不再显示mysql数组数据

时间:2015-12-25 23:24:06

标签: 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 ($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>";

        }

    }

}

然后从产品页面和it works as intended调用此函数。

但是当我向函数添加分页时(在ehow教程之后),该函数没有任何输出(see here)。

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>';

        }
    }
}

我测试了我的sql查询,它在mysql Workbench中工作正常。 我究竟做错了什么?任何人都可以推荐更好的方法吗?

1 个答案:

答案 0 :(得分:0)

创建查询时,您将封装偏移量和单引号之间的限制(如果我正确读取您的代码)。

SELECT * FROM product_reviews WHERE product_reviews.productID='25' LIMIT '0','3'

尝试删除这些单引号。