Click here to Skip to main content
15,395,065 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi as i have mentioned in previous questions I am a beginner in PHP, I have to design a website which can view a database, add and delete records from the database.

I keep getting the following error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\View.php on line 51


The following is the code for viewing the database (View.PHP):

<html>

<head>

<title>View Records</title>

</head>

<body>

<?php

/*

View.PHP

Displays all data from 'productorders' table

*/
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "stationaryonlinecustomers";

 // connect to database
 $con = mysqli_connect("localhost","root","") or die("Error");


# get results from database
$i ="SELECT * FROM productorders";
$result = mysqli_query($con, $i);
#or die(mysql_error());


# display data in table

echo "<p>View All" ;


echo "<table border='1' cellpadding='10'>";

echo "<tr> <th>ID</th> <th>Product Name</th> <th>Price</th> <th>Stock</th> <th></th></tr>";


# loop through results of database query, displaying them in the table
echo $result;
if (mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {

# echo out the contents of each row into a table

echo "<tr>";

echo "<td>" . $row['ID']. "</td>";

echo "<td>" . $row['Product Name']. "</td>";

echo "<td>" . $row['Price']. "</td>";

echo "<td>" . $row['Stock']. "</td>";

echo "<td><a href='edit.php'> Edit</a></td>";

echo "<td><a href='delete.php'> Delete</a></td>";

echo "</tr>";

    }
}
echo "</table>";

?>

<a href="New.php">Add a new record</a></p>
<a href="New.php">Delete a record</a></p>

</body>

</html>


Why is this happening and is the solution to fixing this error ?

If anyone can help me I would really Appreciate it.

What I have tried:

Have followed online video tutorials on youtube, PHP Guides, websites and books.
I have consulted fellow colleagues but are unable to help remove this error.
Posted
Updated 25-Jul-21 0:55am
Comments
Leo Chapiro 7-Mar-18 10:43am
   
I have also a question: How to avoid re-post?
Richard MacCutchan 7-Mar-18 10:58am
   
Please go and read the documentation; the error is clearly explained there.

This is the third time you have posted this question and the answer remains the same: read the documentation.
   
try modifying the @result line:

$result = mysqli_query($con, $i)or die("Error")
   
it's because the query is false: Common problems copy pasting code from other parts of program or file. Typing mistakes, on table or column names. Forgetting to select the column or columns you want. [ not always a good idea to automatically select all with a * wild card anyway] Problems with separating commas,in value list etc, this actually happened to me yesterday, took me three days to visually spot it.
(update X set y ='a' where Z="what I was searching for username etc" y1="the other value I want to search for" y2="the final value I was searching for";

Gave all sorts of errors then I spotted what I had forgotten and hey presto problem instantly solved.

(update X set y ='a' where Z="what i was searching for username etc", y1="the other value I want to search for", y2="the final value I was searching for";

JUST THOUGHT of something else "try adding a WHERE clause at end of query", the very essence of relational databases is to tell the program where to look at what to look search for. So it need not be a single value selecting only one COLUMN. It can be a value in another column that tells the query when to stop, you need tottery and prevent a situation where you spill out millions of records to the public etc. WHERE control_column= "OK" good you can display this.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900