Help with search engine

C++, C#, Java, PHP, ect...
Post Reply
acer360
Posts: 32
Joined: Sun Nov 29, 2009 12:48 pm

Help with search engine

Post by acer360 »

hey i got need a little help with this code i made for a search engine i checked every thing is ok with the database for it and yet i just can't get it to search oh and insted of getting an error and what line the error was on i just get
half of my search.php code. here is the search.php code:

Code: Select all

 <php?
session_start();
include "connect.php";

$button = $_GET['submit'];
$search = $_GET['search'];
{
if (!$button)
   echo "You didn't submit a keyword.";
}
else
{
    if (strlen($search)<=2);
       echo "Search term too short.";
    else
    {
        echo "you searched for <b>$search</b> <hr size='1'>";
        
        mysql_connect("localhost","root","")
        mysql_select_db("asearch");

        {
          
          $search_exploded = explode(" ",$search);
          foreach($search_exploded as $search_each)
          {
                                   $x++;
                                   if ($x=1)
                                      $construct .= "keywords LIKE '%$search_each%'";
                                   else
                                       $construct .= "OR keywords LIKE '%$search_each%'";


          }
          {


          $construct = "SELECT * FROM asearch WHERE $construct";
          $run = mysql_query($construct):
          
          if mysql_num_rows($run)=0)
             ehco "No results found";
          else
        }
            echo "$foundnum results found!<p>";
            
            while ($runrows = mysql_fetch_assoc($run))
            {
              $title = $runrow['title'];
              $desc = $runrow['description'];
              $url = $runrow['url'];
              
              echo "
              <b>$title</b><br>
              $desc<br>
              <a href-'$url'>$url<p>
              ";

          }


    }
}
?> 
XBOX 4 EVER
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Re: Help with search engine

Post by MAruz »

There's a few errors here.

First one is the opening php tag. should be <?php instead of <php?

Second one is:
{ <-- this curly bracket is at the wrong place
if (!$button) <--- should be here
echo "You didn't submit a keyword.";
}

correct is:
if (!$button) {
echo "You didn't submit a keyword.";
}

third error is:
if (strlen($search)<=2); <-- this semi colon should be deleted
echo "Search term too short.";

correct is (I also prefer to use curly brackets, even though it's not strictly needed in this case)
if (strlen($search)<=2)
echo "Search term too short.";

fourth error is:
mysql_connect("localhost","root","") <--- missing semi colon

fifth error is:
$run = mysql_query($construct): <--- replase colon with semi colon

sixth error is:
if mysql_num_rows($run)=0) <--- missing an opening parentheses
ehco "No results found"; <--- also, echo is spelled wrong

correct is:
if (mysql_num_rows($run)=0)
echo "No results found";

seventh error is a bit harder to point out like I did with the others, it's a totally misplaced curly bracket...
So I'm rather posting the entire fixed up code below:
acer360 wrote:

Code: Select all

 <?php
session_start();
include "connect.php";

$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
{
	echo "You didn't submit a keyword.";
}
else
{
	if (strlen($search)<=2)
	{
		echo "Search term too short.";
	}
	else
	{
		echo "you searched for <b>$search</b> <hr size='1'>";

		mysql_connect("localhost","root","");
		mysql_select_db("asearch");

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

		foreach($search_exploded as $search_each)
		{
			$x++;
			if ($x=1)
			{
				$construct .= "keywords LIKE '%$search_each%'";
			}
			else
			{
				$construct .= "OR keywords LIKE '%$search_each%'";
			}
		}

		$construct = "SELECT * FROM asearch WHERE $construct";
		$run = mysql_query($construct);

		if (mysql_num_rows($run)=0)
		{
			echo "No results found";
		}
		else
		{
			echo "$foundnum results found!<p>";
		}
		while ($runrows = mysql_fetch_assoc($run))
		{
			$title = $runrow['title'];
			$desc = $runrow['description'];
			$url = $runrow['url'];
	
			echo "
				<b>$title</b><br>
				$desc<br>
				<a href-'$url'>$url<p>
				";
		}
	}
}
?>
 
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
Post Reply

Return to “Coding”