Fatal error which fetching data from mysqli with limit

87 views Asked by At

Set pagination, on $result1 are fetching all rows from table and, $new_result set its LIMIT. But i got an error "Fatal error: Call to a member function fetch_assoc() on a non-object in /home/...."

$page_limit = 28;
$table_name_3 = 'table';

if (!isset($_GET['page']) )
    { $start=0; } else
    { $start = ($_GET['page'] - 1) * $page_limit; }

$mysqli = new mysqli("xxxxxxx" , "xxxxxxx" , "xxxxxxx" , "xxxxxxxxx");
$result1 = $mysqli->query( "SELECT * FROM $table_name_3 
                 WHERE `categories` LIKE  '$newq'
                 AND   `inStock` LIKE 'true' ");
while ( $rows =  $result1->fetch_assoc() ) {

$new_result = $mysqli->query( $rows . "LIMIT $start,$page_limit " );
while ( $rows =  $new_result->fetch_assoc() ) {

$total = mysqli_num_rows($result);
$store = $rows['store'];
$productId = $rows['productId'];
2

There are 2 answers

0
Suat Hyusein On

This is not the right way to do it, as your $rows object contains an array of fetched results -> $row['columnname'], $row['columnname2'] and so on. Instead, use it in one query :

"SELECT * FROM $table_name_3 WHERE `categories` LIKE  '$newq' 
                            AND `inStock` LIKE 'true' 
                            LIMIT ($page_limit-$offset) OFFSET $offset"
2
Mokkun On

You code doesn't make much sense, I don't understand why two requests and I don't understand the 2nd request at all, have you tried with one request?

SELECT * FROM $table_name_3
 WHERE 
`categories` LIKE  '$newq' 
 AND `inStock` LIKE 'true' 
 LIMIT $start,$page_limit