Jump to content

search engine


etsted

Recommended Posts

this script is supposed to let me search by multiple keywords, but only lets me search by one at a time. The error i get when i try to type in several keywords is:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwsearch.php on line 32

 

<h1>search engine</h1>

<form action="search.php" method="get">
<input type="text" name="k" size='50' />
<input type="submit" name="submit" value='Search' />
</form>
<hr />
<?php
include "connect.php";
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach($terms as $each) {
$i = 0;
$i++;
if($i == 1) {
$query .= "keywords LIKE '%$each%' ";
} else {
$query .= "OR keywords LIKE '%$each%' ";
}
}
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if($numrows > 0)
{
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
if($k == "") {
echo "";
} else {
echo "<h2><a href='$link'>$title</a></h2>
$description";
}
}
}
else
{
echo "no result on $k";
}
Link to comment
Share on other sites

this time i used a mysqli_error() to check for errors on a new script, and got this message:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description LIKE '%w%'' at line 1

 

I used the following search engine code below.

 

<h1>search engine</h1>
<form action="search.php" method="get">
<input type="text" name="k" size='50' />
<input type="submit" name="submit" value='Search' />
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM videos WHERE ";
foreach($terms as $each) {
$i = 0;
$i++;
if($i == 1) {
$query .= "description LIKE '%$each%' ";
} else {
$query .= "OR description LIKE '%$each%' ";
}
}
include "connect.php";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$numrows = mysqli_num_rows($result);
if($numrows > 0)
{
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
$description = $row['description'];
if($k == "") {
echo "";
} else {
echo "<h3><a href='$url'>$title</a></h3>
$description";
}
}
} else {
echo "no results on $k";
}
?>
Link to comment
Share on other sites

it didnt work. now i get the error message all the time.

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR description LIKE '%%'' at line 1

Link to comment
Share on other sites

sounds like you don't have anything in your LIKE clause. try printing the full query and debugging your loop to make you're code is doing what you think it's doing. if you are having problems, please post the updated / changed code.

Edited by thescientist
Link to comment
Share on other sites

new code:

 

Error:

SELECT * FROM videos WHERE OR description LIKE '%%'

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR description LIKE '%%'' at line 1

 

 

<h1>Search:</h1>
<form action="search.php" method="get" class="search">
<input type="text" name="k" size='50' />
<input type="submit" name="submit" value='Search' />
</form>
<?php
$username = $_SESSION['username'];
if(isset($username))
{
include "navigator/navigator_login.php";
} else {
include "navigator/navigator.php";
}
?>
<hr />
<?php
include "connect.php";
$k = mysqli_real_escape_string($con, htmlentities($_GET['k']));
$terms = explode(" ", $k);
$query = "SELECT * FROM videos WHERE ";
$i = 0;
foreach($terms as $each) {
$i++;
if($i == 0) {
$query .= "description LIKE '%$each%' ";
} else {
$query .= "OR description LIKE '%$each%' ";
}
}
print_r($query);
echo "<br />";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$numrows = mysqli_num_rows($result);
if($numrows > 0)
{
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
$description = $row['description'];
if($k == "") {
echo "";
} else {
echo "<h3><a href='$url'>$title</a></h3>
$description";
}
}
} else {
echo "no results on $k";
}
mysqli_close($con);
?>
Link to comment
Share on other sites

okey so i think its fixed, but every time i add only blank space, all the data from my database is displayed. how can i prevent that?

 

new code:

 

<form action="search.php" method="get" class="search">
<input type="text" name="k" size='50' />
<input type="submit" name="submit" value='Search' />
</form>
<?php
$username = $_SESSION['username'];
if(isset($username))
{
include "navigator/navigator_login.php";
} else {
include "navigator/navigator.php";
}
?>
<hr />
<?php
include "connect.php";
$k = mysqli_real_escape_string($con, htmlentities($_GET['k']));
$terms = explode(" ", $k);
$query = "SELECT * FROM videos WHERE ";
$i = 0;
foreach($terms as $each) {
$i++;
if($i == 1) {
$query .= "description LIKE '%$each%' ";
} else {
$query .= "OR description LIKE '%$each%' ";
}
}
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$numrows = mysqli_num_rows($result);
if($numrows > 0)
{
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
$url = $row['url'];
$description = $row['description'];
if($k == "") {
echo "";
} else {
echo "<h3><a href='$url'>$title</a></h3>
$description";
}
}
} else {
echo "no results on $k";
}
mysqli_close($con);
?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...