What is the error in these PHP codes? (Warning: mysqli_num_rows () exp ..) (Warning: mysqli_fetch_array () exp ...)

6

I'm following a tutorial to create an feed on Android. I have already created the database and I have uploaded the PHP scripts to the host, but I get these errors:

  

Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean given in /home/u216588924/public_html/feed.php on line 16

     

Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given in /home/u216588924/public_html/feed.php on line 36 []

Here are the codes:

dbConnect.php

    
<?php
define('HOST','mysql.hostinger.mx');
define('USER','<usuario>');
define('PASS','<password>');
define('DB','<base>');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

feed.php

<?php 

//Getting the page number which is to be displayed  
$page = $_GET['page'];  

//Initially we show the data from 1st row that means the 0th row 
$start = 0; 

//Limit is 3 that means we will show 3 items at once
$limit = 3; 

//Importing the database connection 
require_once('dbConnect.php');

//Counting the total item available in the database 
$total = mysqli_num_rows(mysqli_query($con, "SELECT id from feed "));

//We can go atmost to page number total/limit
$page_limit = $total/$limit; 

//If the page number is more than the limit we cannot show anything 
if($page<=$page_limit){

    //Calculating start for every given page number 
    $start = ($page - 1) * $limit; 

    //SQL query to fetch data of a range 
    $sql = "SELECT * from feed limit $start, $limit";

    //Getting result 
    $result = mysqli_query($con,$sql); 

    //Adding results to an array 
    $res = array(); 

    while($row = mysqli_fetch_array($result)){
        array_push($res, array(
            "name"=>$row['name'],
            "publisher"=>$row['publisher'],
            "image"=>$row['image'])
            );
    }
    //Displaying the array in json format 
    echo json_encode($res);
}else{
        echo "over";
}

?>
    
asked by user21032 30.10.2016 в 05:39
source

3 answers

3

The warnings come out because mysqli_num_rows() I return a false , which means that your query has a failure (ex: can not find the field or table).

To know and handle the fault you can use the following code:

if ($result) {

    $total = mysqli_num_rows($result);

} else {

    die(mysqli_error($con));    
}

Your whole code:

 <?php
$page = $_GET['page'];    
$start = 0;    
$limit = 3;
require_once('dbConnect.php');

$result1 = mysqli_query($con, "SELECT id from feed");

if (!$result1) {

    // Mostrar error o hacer otra cosa
    die(mysqli_error($con));

} else {

    $total = mysqli_num_rows($result1);        
    $page_limit = $total / $limit;

    if ($page <= $page_limit) {

        $start = ($page - 1) * $limit;            
        $sql = "SELECT * from feed limit $start, $limit";            
        $result2 = mysqli_query($con, $sql);

        if (!$result2) {

            // Mostrar error o hacer otra cosa
            die(mysqli_error($con));

        } else {  

            $total = mysqli_num_rows($result2);    

            $res = [];

            while ($row = mysqli_fetch_array($result2)) {
                array_push($res,
                           [
                               "name"      => $row['name'],
                               "publisher" => $row['publisher'],
                               "image"     => $row['image']
                           ]
                );
            }
            echo json_encode($res);
        } 

    } else {
        echo "over";
    }    
}
    
answered by 30.10.2016 в 16:14
0

The error can be in two cases:

  • The query is wrong and gives mysql error, so the code: while($row = mysqli_fetch_array($result)){ gives error
  • Either it does not return results. For this case I recommend you use this if : if ($result->num_rows > 0) { this you have to put before while
  • answered by 30.10.2016 в 10:02
    0

    You are doing wrong the query SQL if you want the total number of records you should do:

    SELECT COUNT(column_name) FROM table_name;

        
    answered by 10.01.2017 в 13:09