+ Reply to Thread
Results 1 to 10 of 10

Thread: PHP/MySQL Search Engine

  1. #1
    o0slowpaul0o's Avatar
    o0slowpaul0o is offline x10 Sophmore o0slowpaul0o is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    127

    PHP/MySQL Search Engine

    In this tutorial I will show you how to make a fully function search for your database. This tutorial uses "LIKE $search" Why? Because some people will search with more than one word and if they search for say three words then it will search the database for those three words in exact order just how they searched it, which is no good if they searched keywords like "php database mysql" which would never be a sentence that you would find.

    1. First we should create a table (to use as an example) for holding people's names and a little about them.

    Code:
    <?php
    // set your infomation.
    $dbhost='localhost';
    $dbusername='username';
    $dbuserpass='password';
    $dbname = 'database';
    
    // connect to the mysql database server.
    mysql_connect ($dbhost, $dbusername, $dbuserpass);
    //select the database
    mysql_select_db($dbname) or die('Cannot select database');
    
    //create the table, customize it if you want
    $query = 'CREATE TABLE people(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    name VARCHAR(30) NOT NULL,
    about TEXT NOT NULL)';
    $result = mysql_query($query);
    echo "Table Created!";
    ?>
    2. Next we'll insert make a page where we can add people and information about them. Simply just call this file add.php.

    Code:
    <center>
    <?php
    // set your infomation.
    $dbhost='localhost';
    $dbusername='username';
    $dbuserpass='password';
    $dbname = 'database';
    
    // connect to the mysql database server.
    mysql_connect ($dbhost, $dbusername, $dbuserpass);
    //select the database
    mysql_select_db($dbname) or die('Cannot select database');
    
    if ($_POST['name']) {
    $name = $_POST['name'];
    if ($_POST['about']) {
    $about = $_POST['about'];
    $query = "INSERT INTO people (name,about) VALUES ('".$name."','".$about."')";
    mysql_query($query)or die(mysql_error());
    echo $name." was inserted successfully";
    }else{
    echo "About was left blank";
    }
    }
    ?>
    <h2>Add</h2>
    <form action='add.php' method='POST'>
    Name: <input type='text' name='name' size='33' /><br />
    About: <textarea name='about' cols='25' rows='5'/></textarea><br />
    <input type='submit' value='Add!' />
    </form>
    <a href="search.php">Search for a person.</a>
    </center>
    Now onto the actual searching, create a php page called search.php and use this code.

    Code:
    <?php
    // set your infomation.
    $dbhost='localhost';
    $dbusername='username';
    $dbuserpass='password';
    $dbname = 'database';
    
    // connect to the mysql database server.
    mysql_connect ($dbhost, $dbusername, $dbuserpass);
    //select the database
    mysql_select_db($dbname) or die('Cannot select database');
    
    $keywords = explode(" ", $search);
    
    $query = "SELECT id,name,about FROM people " .
    "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(mysql_error());
    ?>
    <center>
    <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;">
    <?php
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td style='border-bottom: 1px solid #000000;'>";
    echo "<b>".$row['name']."</b>";
    echo "<p>".$row['about']."</p>";
    echo "</td>";
    echo "</tr>";
    }
    ?>
    </td>
    </tr>
    </table>
    <a href="add.php">Add a person to our index.</a>
    </center>

  2. #2
    Akkarin's Avatar
    Akkarin is offline x10 Elder Akkarin is an unknown quantity at this point
    Join Date
    May 2005
    Location
    London, UK
    Posts
    827

    Re: PHP/MySQL Search Engine

    Another great and useful tutorials by slowpaul! Good job man!

  3. #3
    bavuong is offline x10Hosting Member bavuong is an unknown quantity at this point
    Join Date
    Jul 2005
    Posts
    11

    Re: PHP/MySQL Search Engine

    thanks for sharring this is what i need

  4. #4
    n4tec's Avatar
    n4tec is offline Lord Of The Keys n4tec is an unknown quantity at this point
    Join Date
    Feb 2005
    Location
    GeT NOTICED via n4tec
    Posts
    1,656

    Re: PHP/MySQL Search Engine

    o0slowpaul0o, thanx for the tutorial once again.. You really have very good utorials.. Keep it up..
    .::: Regards, n4tec :::.


  5. #5
    o0slowpaul0o's Avatar
    o0slowpaul0o is offline x10 Sophmore o0slowpaul0o is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    127

    Re: PHP/MySQL Search Engine

    Donate points! Make better if you do. :P
    Last edited by o0slowpaul0o; 07-31-2005 at 09:57 AM.

  6. #6
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: PHP/MySQL Search Engine

    You shoudl try to teach "what is being actually done", so people that don't know a lot of PHP can learn.

    Just a suggestion, other then that, it's a pretty cool idea.

  7. #7
    o0slowpaul0o's Avatar
    o0slowpaul0o is offline x10 Sophmore o0slowpaul0o is an unknown quantity at this point
    Join Date
    Mar 2005
    Posts
    127

    Re: PHP/MySQL Search Engine

    I was recommended that to a friend. Asked him, should I post down verables and tell them what they mean, just so you can learn PHP.

  8. #8
    bdweb is offline x10Hosting Member bdweb is an unknown quantity at this point
    Join Date
    Jul 2005
    Posts
    15

    Re: PHP/MySQL Search Engine

    Thanks for your nice tutorial
    I have question in serch option

    When I create a check box in a forum if the user did not select the check box click submit it shows me,
    Notice: Undefined index: cgi in c:\program files\easyphp1-8\www\webhosting\view.php on line 9

    I use this funtion to call the variable

    $php = $HTTP_POST_VARS['php'];

    view.php

    Which command or fution shall I use for check box
    Can any one help me about this

    Thanks in adv

  9. #9
    *=Matt=* is offline x10Hosting Member *=Matt=* is an unknown quantity at this point
    Join Date
    Aug 2005
    Location
    Minnesota, USA
    Posts
    16

    Re: PHP/MySQL Search Engine

    about time lol I needed a tut like this for some search page. Thank You

  10. #10
    Articz is offline x10 Lieutenant Articz is an unknown quantity at this point
    Join Date
    Jan 2005
    Posts
    432

    Re: PHP/MySQL Search Engine

    nice one there slow thx for shareing will have to try this on out

+ Reply to Thread

Similar Threads

  1. Effectiveness of Search Engine Optimization
    By Rhianna in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 11-15-2009, 02:29 PM
  2. Search Rage!
    By Joker Boy in forum Crossfire
    Replies: 21
    Last Post: 10-15-2006, 01:45 AM
  3. Search Engine Ad URL
    By James in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 05-02-2006, 05:11 AM
  4. What Search Engine do you use?
    By MicrotechXP in forum Off Topic
    Replies: 35
    Last Post: 08-12-2005, 07:27 AM
  5. any tips on how Search Engine Optimization my site?
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 6
    Last Post: 07-10-2005, 11:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers