Database > General Database

Search Engine for Website

(1/1)

Bryant:
Hi

I'm a total noob. I'm trying to create a search engine that retrieves pages from just my website. I created the MySQL database with the appropriate columns (id, title, description, keywords, url) but there is just one error with my PHP coding that I just can't figure out.

The error in Dreamweaver is shown on line 49 where the "else" is at the very bottom, followed by the "echo".

Do you have an idea as to why there is an error?

Thank you!


--- PHP Code: ---<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Engine</title>
</head>
<body>

<center>
<h1>Search</h1>
<form action='search.php' method='GET'>
<input type='text' name='search' size='90' value='<?php echo $_GET['search']; ?>'  /> 
<input type='submit' name='submit' value='Search' >
</center>
</form>
<hr />
<?php
$search = $_GET['search'];
$terms = explode(" ", $search);
$query = "SELECT * FROM search WHERE ";

foreach ($terms as $each){
$i++;
if ($i == i)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}

// connect
mysql_connect("localhost", "database_name", "password");
mysql_select_db("database_name"");

$query = mysql_query($query);
$numrows = mysql_num_rows($query);

if ($numrows > 0) {

while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$url = $row['url'];

echo "<h2><a href='$url'>$title</a></h2>
$description<br /><br />";
}
else
echo "No results found for \"<b>$search</b>\"";
//disconnect 
mysql_close();
}
?>
</body>
</html>


--- End code ---

RaythXC:
There were a few errors in that. Here's a fix


--- PHP Code: ---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Engine</title>
</head>
<body>

<center>
<h1>Search</h1>
<form action='search.php' method='GET'>
<input type='text' name='search' size='90' value='<?php echo $_GET['search']; ?>'  /> 
<input type='submit' name='submit' value='Search' />
</form>
</center>
<hr />
<?php
$search = $_GET['search'];
$terms = explode(" ", $search);
$query = "SELECT * FROM search WHERE ";

foreach ($terms as $each){
$i++;
if ($i == i)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}

// connect
mysql_connect("localhost", "database_name", "password");
mysql_select_db("database_name");

$query = mysql_query($query);
$numrows = mysql_num_rows($query);

if ($numrows > 0) {

while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$url = $row['url'];

echo "<h2><a href=\"{$url}\">{$title}</a></h2>
{$description}<br/><br/>";
            }
        }
else {
echo "No results found for <b>$search</b>"; }
//disconnect 
mysql_close();

?>
</body>
</html>

--- End code ---


The main ones were extra " signs, or bracket mismatching.

Bryant:
OMG!!! It's fixed. I finally have a working search engine for my site. Now all I have to do is make it look pretty.

You were very helpful. Thank you so much for your help. I highly appreciate it.  ;D

Navigation

[0] Message Index

Go to full version