struggling with my web design assignment. I've been following a tutorial to add in a search feature for my website, but I've been getting the following error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /search.php on line 31
line 31 is (or was)
<pre>if(mysqli_num_rows($results) >= 1)</pre>
That was the original error. as per instructions in the comments, I've since revised the code:
<pre>
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyword']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter the name/brand of what you're looking for.";
exit();
}
//database connection info
$host = "localhost";
$db_name = "sookehhh_shopsy_db";
$username = "sookehhh_shopsy";
$password = "xxxx";
//connecting to server and creating link to database
$link = mysqli_connect($host, $username, $password, $db_name) or die('Could not connect: ' . mysqli_connect_error());
//MYSQL search statement
$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
// original query$query = "SELECT * FROM sookehhh_shopsy_db WHERE name LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
//added suggestion below - not sure if correct place?
if (!$result) {
die(mysqli_error($link));
}
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Product Name: " . $row['name'] . "<br />";
$output .= "Price: " . $row['price'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for that item " . $searchTerm;
?>
</pre>
made necessary changes and updated yet again -
now the only error message I'm getting here is "Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist"
I'm assuming that I need to change the username, perhaps because it's too similar?
Anywho, thanks for your help so far, and I apologise for my complete ignorance.
I've been trying to teach myself, but unfortunately time is a luxury I just don't have at the moment.
mysqli_num_rows takes only one parameter a mysqli_result, so you need to remove the connection $rf_koneksi from your call.
The mysqli_num_rows() function returns the number of rows in a result set.
The mysqli_result class ¶Represents the result set obtained from a query against the database.
Return Values ¶ For successful queries which produce a result set, such as SELECT, SHOW, DESCRIBE or EXPLAIN , mysqli_query() will return a mysqli_result object. For other successful queries, mysqli_query() will return true .
The problem is your query returned false
meaning there was an error in your query. After your query you could do the following:
if (!$result) {
die(mysqli_error($link));
}
Or you could combine it with your query:
$results = mysqli_query($link, $query) or die(mysqli_error($link));
That will print out your error.
Also... you need to sanitize your input. You can't just take user input and put that into a query. Try this:
$query = "SELECT * FROM shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'";
In reply to: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist
Are you sure the table name is sookehhh_shopsy_db? maybe it's really like users or something.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With