Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in [duplicate]

Tags:

php

mysql

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.

like image 843
Chrissy Avatar asked May 26 '13 05:05

Chrissy


People also ask

How many parameters are expected in mysqli_num_rows () function?

mysqli_num_rows takes only one parameter a mysqli_result, so you need to remove the connection $rf_koneksi from your call.

What is the use of mysqli_num_rows?

The mysqli_num_rows() function returns the number of rows in a result set.

What is mysqli_result?

The mysqli_result class ¶Represents the result set obtained from a query against the database.

What is the return type of Mysqli_query?

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 .


1 Answers

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.

like image 58
chrislondon Avatar answered Nov 15 '22 16:11

chrislondon