What is the error in these PHP codes? (Warning: mysqli num rows () exp..) (Warning: mysqli fetch array () exp…)
I am following a tutorial to create a feed on Android. I have already created the database and 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 online 36 []
Here I leave 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";
}
?>
3 answers
The warnings come out to you because mysqli_num_rows()
I return you a false
, which means that your query
has a bug (e.g. it does not find the field or table).
To know and handle the bug you can use the following code:
if ($result) {
$total = mysqli_num_rows($result);
} else {
die(mysqli_error($con));
}
Your integer 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";
}
}
The error can be for two cases:
- the query is poorly done and gives mysql error, so the code:
while($row = mysqli_fetch_array($result)){
gives error - either does not return results. For this case I recommend you use this
if
:if ($result->num_rows > 0) {
you have to put this beforewhile
You are doing wrong query SQL
if you want the total number of records you should do:
SELECT COUNT(column_name) FROM table_name;