Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean is set to

struggling with my web design appointment. I am following the tutorial to add a search function for my website, but I am getting the following error:

Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, boolean is set in /search.php on line 31

line 31 is (or was)

<pre>if(mysqli_num_rows($results) >= 1)</pre> 

It was an original mistake. in accordance with the instructions in the comments, I have 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 the necessary changes and updated again -

now the only error message I get here is "Table" sookehhh_shopsy_db.sookehhh_shopsy_db "does not exist"

I assume that I need to change the username, perhaps because it is too similar?

Anywho, thanks for your help so far, and I apologize for my complete ignorance.

I tried to teach myself, but, unfortunately, time is a luxury that I simply do not own at the moment.

+6
source share
1 answer

The problem is that your request is false , which means there was an error in your request. After your request, you can do the following:

 if (!$result) { die(mysqli_error($link)); } 

Or you can combine it with a request:

 $results = mysqli_query($link, $query) or die(mysqli_error($link)); 

This will print your error.

Also ... you need to misinform your entrance. You cannot just enter user input and put it in a query. Try the following:

 $query = "SELECT * FROM shopsy_db WHERE name LIKE '%" . mysqli_real_escape_string($link, $searchTerm) . "%'"; 

In response to: Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' does not exist

Are you sure the table name is sookehhh_shopsy_db? perhaps users really like it or something.

+15
source

Source: https://habr.com/ru/post/945856/


All Articles