my search engine

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
acer360
Posts: 32
Joined: Sun Nov 29, 2009 12:48 pm

my search engine

Post by acer360 »

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:

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>
              ";

          }


    }
}
?> 
and i dubble checked all the sql dump is there.pliz help
XBOX 4 EVER
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: my search engine

Post by Chris »

<php? should be <?php

Your code also contains a lot of pointless {'s. Like on line 7.

I cleaned up your whole code, read through it and you might learn a bit.

Code: Select all

<?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.
User avatar
ier
Posts: 67
Joined: Sun Jan 10, 2010 7:32 pm

Re: my search engine

Post by ier »

i got also a script like that from php academy
this is my script the thing are in belgiês

Code: Select all

<?php

$button = $_GET['submit'];
$search = $_GET['search'];
if (!$button)
echo "Je hebt geen tekst ingegeven!";
else
{
if (strlen($search)<1)
echo "Je hebt geen tekst ingegeven!";
else
{
echo "Je zocht naar <strong>$search</strong><hr size='1'>";


mysql_connect("localhost","root","gagip7wq");
mysql_select_db("searchengine");


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

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



}

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

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Geen resultaat voor $search";
else
{
echo "$foundnum resultaat(en) voor <strong>$search</strong><p>";

while ($runrows = mysql_fetch_assoc($run))
{
$title = $runrows['title'];
$description = $runrows['description'];
$url = $runrows['url'];

echo "
<b>$title</b><br>
$description<br>
<a href='$url'>$url</a><p>";

}

}
}
}

?>
<a href="index.php">ga terug naar sooter</a>
Post Reply

Return to “Advanced Help and Support”