how to search data base with php ?

Discussion in 'PHP' started by Funk-woo10, Nov 28, 2007.

  1. #1
    Hi,

    How would I set up a search type engine to look for members on my site ?

    For example search for member

    by name:
    by age:
    by location:

    ect

    and how to display results ?
     
    Funk-woo10, Nov 28, 2007 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you know what your current user table looks like? Knowing the table name and columns someone should be able to come up with a few lines of php for you to just toss onto your site to have a search function. If you don't know the table structure, are you using a software package for your site, like phpbb or something? In that case the table structure is pretty well known already.
     
    drunnells, Nov 28, 2007 IP
  3. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Lets say the table is called members,

    It has three columns, Name, Age Location
     
    Funk-woo10, Nov 28, 2007 IP
  4. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is a little search page for you:

    <?php
    $db_host = 'localhost';
    $db_user = 'my_db_user';
    $db_pw = 'my_db_password';
    $db = 'site_db';
    ?>
    <html>
    <body>
    <center>
    <h2>My Sites Search Page</h2>
    <p>
    
    <?php
    switch ($_REQUEST['action']) {
            case 'get_results':
                    $hDb = mysql_connect($db_host, $db_user, $db_pw);
                    mysql_select_db ($db, $hDb);
                    print get_results($hDb,$search_for);
                    break;
            default:
                    print "<form action=" . $PHP_SELF . ">";
                    print "<input type=hidden name=action value=get_results>";
                    print "Search: <input type=text size=10 name=search_for>";
                    print "<input type=submit>";
                    print "</form>";
                    break;
    }
    
    function get_results($hDb,$search_for) {
            $query = "SELECT * FROM members WHERE "
                    . "name LIKE '%" . mysql_real_escape_string($search_for) . "%' OR "
                    . "location LIKE '%" . mysql_real_escape_string($search_for) . "%'";
            $result = mysql_query($query,$hDb);
            $result_counter = 0;
            if ($result) {
                    $return_table = "<table border=1>";
                    $return_table .= "<tr><td>Name</td><td>Age</td><td>Location</td></tr>";
                    while ($row = mysql_fetch_object($result)) {
                            $result_counter++;
                            $return_table .= "<tr><td>" . $row->name . "</td>";
                            $return_table .= "<td>" . $row->age . "</td>";
                            $return_table .= "<td>" . $row->location . "</td></tr>";
                    }
                    $return_table .= "</table>";
            }
            return $return_table;
    }
    ?>
    <p>
    <b>My sites footer.</b>
    </body>
    </html>
    PHP:
    If you save this to something like "search.php" and open it up in your browser you will get a search box. Entering any text in the search box and pressing the submit button will return you an html table with the name, age and location of any members that have a name or location that matches your search text.

    Of course this is an extremely simplified search page, but hopefully it is enough to get you started.
     
    drunnells, Nov 28, 2007 IP