7

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.

Chrissy
  • 107
  • 1
  • 1
  • 3

1 Answers1

17

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.

chrislondon
  • 12,487
  • 5
  • 26
  • 65
  • Thanks for the helpful information. Unfortunately, I'm still getting another error message. Please see revised post for details. – Chrissy May 26 '13 at 06:34
  • sorry that's my bad. I've updated my mysqli_real_escape_string function – chrislondon May 26 '13 at 06:39
  • It needs to be mysqli_real_escape_string($link, $searchTerm) – chrislondon May 26 '13 at 06:40
  • Thanks! Now I'm just getting " Table 'sookehhh_shopsy_db.sookehhh_shopsy_db' doesn't exist". – Chrissy May 26 '13 at 06:47
  • That means the table you are trying to select doesn't exist. You have the table name the same as the database name. Is that correct? Maybe you have a table like `users` or something? – chrislondon May 26 '13 at 06:49
  • oh god I'm such a noob. it finally clicked that the database name and table name are different things. Thanks for everything! I've added in the correct table name and now I'm just getting a blank page instead of error messages... which is good... but also bad.... – Chrissy May 26 '13 at 06:56