PDA

View Full Version : Tutorial: Search Engine



Colin-Roberts
30-09-2006, 12:20 PM
Since i don't use my i guess someone else can get something out of it.

Mysql:

CREATE TABLE search(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30) NOT NULL,
url VARCHAR(30) NOT NULL,
about TEXT NOT NULL)';
Run that in phpmyadmin

Index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<link type="text/css" rel="stylesheet" href="/css.css">
</head>
<body>
<div align="center">
<img style="margin: 0px;" width="690" height="150" src="banner url here" alt="" title="">
<br></div>
<center>
<form action="search.php" method="get"><font size="1" face="Verdana"><strong>Search:</strong></font>
<input type="text" size="20" name="search">
<input type="submit" value="Search!"></form>
<table style="border: 1px solid rgb(0, 0, 0);" width="50%"></table><font size="1" face="Verdana"><span class="nav">| <a title="" href="/add.php">Add Your Site</a> | </span></font>
<br></center>
</body>
</html>
Basicly Just the search form

ADD.PHP

<center><?php // set your infomation.
$dbhost='localhost'; // host
$dbusername='colin_shout'; // database username
$dbuserpass='cococo'; // database password
$dbname = 'colin_search'; //database

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
//select the database
mysql_select_db($dbname) or die('Cannot select database');
//submits the form
if ($_POST['name']) {
$name = $_POST['name'];
if ($_POST['about']) {
$about = $_POST['about'];
if ($_POST['url']) {
$url = $_POST['url'];
// inserts the results into mysql $query = "INSERT INTO search (name,about,url) VALUES ('".$name."','".$about."','".$url."')"; mysql_query($query)or die(mysql_error());
echo $name." was inserted successfully";
}else{
echo "About/Url was left blank";
//echos if they missed a feild
}
}
}
?>
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<title></title>
<link type="text/css" rel="stylesheet" href="/css.css">
</head>
<body>
<img style="margin: 0px;" width="690" height="150" src="banner url" alt="" title="">
<br></div>
<form action='add.php' method='POST'>
<font size="1" face="Verdana">Name:</font><input type='text' name='name' size='33' /></br>
<font size="1" face="Verdana">Url:</font><input type='text' name='url' size='33' /></br>
<font size="1" face="Verdana">About:</font><textarea name='about' cols='25' rows='5'/></textarea></br>
<input type='submit' value='Add!' />
</form>
<span class="nav"><font size="1" face="Verdana"><span class="nav">| <a href="/add.php" title="">Add Your Site</a> | <a href="/index.php">Search</a> | </span></font>
</center>
</body>
</html>
make sure to put your details to mysql in.

Search.php

<?php $dbhost='localhost'; //host $dbusername='colin_shout'; //database username $dbuserpass='cococo'; //database pass$dbname = 'colin_search'; //database search mysql_connect ($dbhost, $dbusername, $dbuserpass); mysql_select_db($dbname) or die('Cannot select database'); // connects to database
$rowsPerPage = 5; //amount of search results per page
$pageNum = 1;

if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
if(isset($_GET['search']))
{
$search = $_GET['search'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage; //offsets page number

$keywords = explode(" ", $search); //graps searched word

$query = "SELECT id,name,about,url FROM search " .
"WHERE about LIKE '%".$keywords['0']."%'"; // searches for searched word in database

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND about LIKE '%".$keywords[$i]."%'"; //counts the results
}
$query = $query." ORDER BY id LIMIT $offset, $rowsPerPage"; //orders by rank

$result2 = mysql_query($query) or die(mysql_error()); // kills mysql if query didnt work
?>
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<title></title>
<link type="text/css" rel="stylesheet" href="/css.css">
</head>
<body>
<center>
<img style="margin: 0px;" width="690" height="150" title="" alt="" src="banner url">
</br>
<form method="GET" action="search.php">
<b>Search:</b> <input type="text" name="search" size="20" />
<input type="submit" value="Search!" />
</form>
<table width="50%" style="border:1px solid #000000;">
</body>
</html>
<?php
$keywords = explode(" ", $search);

$query = "SELECT COUNT(id) AS numrows FROM search " . //displays id number
"WHERE about LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND about LIKE '%".$keywords[$i]."%'";
}

$result = mysql_query($query) or die('Error, query failed'); //displays error that query failed
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage); //sets number of pages

$self = $_SERVER['PHP_SELF'];
$nav = '';

for($page = 1; $page <= $maxPage; $page++) // shows list of pages
{
if ($page == $pageNum)
{
$nav .= " $page ";
}
else
{
$nav .= " <a href="$self?page=$page&search=$search">$page</a> "; // sets target for pages
}
}

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href="$self?page=$page&search=$search">[Prev]</a> "; // displays option of prev page

$first = " <a href="$self?page=1&search=$search">[First Page]</a> "; //displays option of first page
}
else
{
$prev = ' ';
$first = ' ';
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href="$self?page=$page&search=$search">[Next]</a> "; //displays option of next page

$last = " <a href="$self?page=$maxPage&search=$search">[Last Page]</a> "; //displays option of last page
}
else
{
$next = ' ';
$last = ' ';
}

while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td style='border-bottom: 1px solid #000000;'>"; //echos tables
echo "<b><a href="".$row['url']."">".$row['name']."</a></b>"; // echos the results name
echo "<p>".$row['about']."</p>"; // echos about the result
echo "<p><a href="".$row['url']."">".$row['url']."</a></p>"; //echos about the url
echo "</td>";
echo "</tr>";
}

echo "<tr>";
echo "<td>";
echo "<center>".$first . $prev . $nav . $next . $last."</center>"; //displays the next/prev/last links made above
echo "</td>";
echo "</tr>";
?>
</td>
</tr>
</table>
</br>
<span class="nav"><font size="1" face="Verdana"><span class="nav">| <a href="/add.php" title="">Add Your Site</a> | </span></font>
</center>
agian make sure to add your details

Css.css

body {
margin: 24px 0px 24px 0px ;
padding: 0px;

}

.border {
border: 1px solid #BBBBBB;
}

.border-a {
border: 1px solid #BBBBBB;
background-color: #EEEEEE;
}

/* Text */

.main {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #333333;
line-height: 16px;
}
.main a:link {
text-decoration: underline;
color: #0000FF;
}
.main a:visited {
text-decoration: underline;
color: #0000FF;
}
.main a:hover {
text-decoration: none;
color: #0000FF;
}
.main a:visited {
text-decoration: none;
color: #0000FF;
}
.lighter {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #666666;
line-height: 16px;
}
.lighter a:link {
text-decoration: underline;
color: #0000FF;
}
.lighter a:visited {
text-decoration: underline;
color: #0000FF;
}
.lighter a:hover {
text-decoration: none;
color: #0000FF;
}

.darker {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #222222;
line-height: 16px;
border: none;
}
.darker a:link {
text-decoration: underline;
color: #0000FF;
}
.darker a:visited {
text-decoration: underline;
color: #0000FF;
}
.darker a:hover {
text-decoration: none;
color: #0000FF;
}

.large {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 18px;
color: #333333;
font-weight:bold;
line-height: 22px;
}

.largewhite {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 18px;
color: #FFFFFF;
font-weight:bold;
line-height: 22px;
}

.medium {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #333333;
font-weight:bold;
line-height: 22px;
}

.mediumwhite {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #FFFFFF;
font-weight:bold;
line-height: 22px;
}

.small {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #333333;
}
.small a:link {
text-decoration: none;
color: #0000FF;
}
.small a:visited {
text-decoration: none;
color: #0000FF;
}
.small a:hover {
text-decoration: underline;
color: #0000FF;
}

.nav {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #CCCCCC;
}
.nav a:link {
text-decoration: none;
color: #666666;
}
.nav a:visited {
text-decoration: none;
color: #666666;
}
.nav a:hover {
text-decoration: none;
color: #333333;
}

.field {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #333333;
}

.white {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
}
.white a:link {
text-decoration: none;
color: #FFFFFF;
}
.white a:visited {
text-decoration: none;
color: #FFFFFF;
}
.white a:hover {
text-decoration: none;
color: #999999;
}

.italic {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
color: #777777;
line-height: 16px;
font-style: italic;
}

There enjoy i hope i commented enough.

uber
30-09-2006, 12:24 PM
Nice +rep

Kymux
30-09-2006, 12:54 PM
+rep gr88 xx

James.Easy
04-11-2006, 12:29 AM
REP+ I needed that!

Tomm
04-11-2006, 08:07 AM
This is okay apart from all it does is add a index of pages, not a search engine. The script does not crawl the pages or anything.

Zaub
04-11-2006, 09:39 AM
Error

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unclosed quote @ 144
STR: '
SQL: CREATE TABLE search( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30) NOT NULL, url VARCHAR(30) NOT NULL, about TEXT NOT NULL)'


SQL query:

CREATE TABLE search( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30) NOT NULL, url VARCHAR(30) NOT NULL, about TEXT NOT NULL)'

MySQL said: Documentation
#1064 - 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 ''' at line 1

That's what happened with mine when i entered the SQL.

Nether$
04-11-2006, 09:42 AM
I have 1 MYSQL database, can i use that database with this even though its still inuse??

Lycan
04-11-2006, 09:53 AM
i woudn't recommend it

Colin-Roberts
04-11-2006, 12:07 PM
ye i made errors in the mysql query ill fix it in a sec & organised ye i realize that but hey it works great for like lyric search engine.

-::Mr.Dave::-
04-11-2006, 12:27 PM
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unclosed quote @ 151
STR: '
SQL: CREATE TABLE search(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30) NOT NULL,
url VARCHAR(30) NOT NULL,
about TEXT NOT NULL)'


SQL query:

CREATE TABLE search( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30) NOT NULL, url VARCHAR(30) NOT NULL, about TEXT NOT NULL)'

MySQL said:

#1064 - 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 ''' at line 6

when i do mine

can osmeone make a crawling spider for this

James.Easy
04-11-2006, 12:30 PM
Sorry, didnt see your post.

Colin-Roberts
04-11-2006, 12:40 PM
CREATE TABLE search(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30) NOT NULL,
url VARCHAR(30) NOT NULL,
about TEXT NOT NULL)';
is the rite code. i hope.

James.Easy
04-11-2006, 12:52 PM
Error

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unclosed quote @ 143
STR: '
SQL: CREATE TABLE search(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(30) NOT NULL,
url VARCHAR(30) NOT NULL,
about TEXT NOT NULL)'


SQL query:

CREATE TABLE search( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30) NOT NULL, url VARCHAR(30) NOT NULL, about TEXT NOT NULL)'

MySQL said: Documentation
#1064 - 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 ''' at line 6

Colin-Roberts
04-11-2006, 12:58 PM
:s mysql aint my thing.
http://www.colin-roberts.net/search.gz
theres a blank mysql database with tables installed.

James.Easy
04-11-2006, 01:07 PM
It sttill doesnt work, ggrr.I really need this.I'm rubbish with mySQL as well.

Colin-Roberts
04-11-2006, 01:11 PM
the mysql has to be fine now did you edit your mysql details into the files

James.Easy
04-11-2006, 01:12 PM
I fixed it, made my own querie.

CREATE TABLE `search` (
`id` int(11) NOT NULL auto_increment,
`url` varchar(30) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`about` varchar(40) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

Search.PHP has a error.

Colin-Roberts
04-11-2006, 01:26 PM
theres search.php code took from my working one..
make sure to add mysql details.

<?php
$dbhost='localhost';
$dbusername='';
$dbuserpass='';
$dbname = '';

mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');

$rowsPerPage = 5;
$pageNum = 1;

if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
if(isset($_GET['search']))
{
$search = $_GET['search'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$keywords = explode(" ", $search);

$query = "SELECT id,name,about,url FROM search " .
"WHERE about LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND about LIKE '%".$keywords[$i]."%'";
}
$query = $query." ORDER BY id LIMIT $offset, $rowsPerPage";

$result2 = mysql_query($query) or die(mysql_error());
?>
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<title>Cj Search - Results</title>
<link type="text/css" rel="stylesheet" href=/v4/search/css.css">
</head>
<body>
<center>
<img style="margin: 0px;" width="690" height="150" title="" alt="" src="http://www.colin.connect-hosting.com/v4/colinrobertsyf1.PNG">
</br>
<form method="GET" action="search.php">
<b>Search:</b> <input type="text" name="search" size="20" />
<input type="submit" value="Search!" />
</form>
<table width="50%" style="border:1px solid #000000;">
</body>
</html>
<?php
$keywords = explode(" ", $search);

$query = "SELECT COUNT(id) AS numrows FROM search " .
"WHERE about LIKE '%".$keywords['0']."%'";

for ($i=1; $i<count($keywords); $i++) {
$query = $query." AND about LIKE '%".$keywords[$i]."%'";
}

$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];
$nav = '';

for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page ";
}
else
{
$nav .= " <a href=\"$self?page=$page&search=$search\">$page</a> ";
}
}

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page&search=$search\">[Prev]</a> ";

$first = " <a href=\"$self?page=1&search=$search\">[First Page]</a> ";
}
else
{
$prev = ' ';
$first = ' ';
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page&search=$search\">[Next]</a> ";

$last = " <a href=\"$self?page=$maxPage&search=$search\">[Last Page]</a> ";
}
else
{
$next = ' ';
$last = ' ';
}

while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td style='border-bottom: 1px solid #000000;'>";
echo "<b><a href=\"".$row['url']."\">".$row['name']."</a></b>";
echo "<p>".$row['about']."</p>";
echo "<p><a href=\"".$row['url']."\">".$row['url']."</a></p>";
echo "</td>";
echo "</tr>";
}

echo "<tr>";
echo "<td>";
echo "<center>".$first . $prev . $nav . $next . $last."</center>";
echo "</td>";
echo "</tr>";
?>
</td>
</tr>
</table>
</br>
<span class="nav"><font size="1" face="Verdana"><span class="nav">| <a href="/v4/search/add.php" title="">Add Your Site</a> | <a href="/terms.htm">Terms</a> | <a href="/credits.htm">Credits</a> |</span></font>
</center>

-::Mr.Dave::-
04-11-2006, 01:32 PM
search.php has error and we need a crawling spider to add sites

Edit: search works now we need a spider to add to it

Want to hide these adverts? Register an account for free!