I recently made a small search engine using php&mysql and i have reached an error, every time i click the "search" button it just displays half of my search.php code.
this is what i have for search.php:
<?php
session_start(); // What's this for? I didn't see it needed anywhere in this script.
if( isset($_GET['search']) )
{
if( strlen($_GET['search']) <= 2 )
{
echo "Search term too short.\n";
}
else
{
echo "you searched for <strong>". htmlentities($search) ."</strong>\n";
include "connect.php"; // Only include this when needed, otherwise it uses extra memory for no reason.
// I'm curious to know what's on connect.php if this is needed here.
mysql_connect("localhost","root","")
mysql_select_db("asearch");
$search_exploded = explode(" ", $search);
$construct = ""; // Please define variables before trying to extend them.
$x = 0; // Or trying to add to them.
foreach( $search_exploded as $search_each )
{
$search_each = addslashes(htmlentities($search_each)); // Please don't allow people to mess around with your database too easy.
$x++;
$construct .= ( ($x == 1) ? "" : "OR " ) . "`keywords` LIKE '%$search_each%'"; // How about that for an easy if statement?
}
$query = mysql_query("SELECT * FROM `asearch` WHERE $construct"):
$found = mysql_num_rows($query);
if ( $found < 1 )
{
echo "No results found.";
}
else
{
echo "$found results found!\n";
while( $array = mysql_fetch_assoc($query) )
{
// Why define so many pointless variables?
# $title = $array['title'];
# $desc = $array['description'];
# $url = $array['url'];
echo "
<p>
<strong>{$array['title']}</strong><br />
{$array['description']}<br />
<a href=\"{$array['url']}\">{$array['url']}</a>
</p>\n";
}
}
}
}
?>
Fighting for peace is declaring war on war. If you want peace be peaceful.