Adblock breaks this site

[PHP] Simple search query

Discussion in 'Guides' started by Natan, Apr 22, 2013.

  1. Natan

    Natan Forum Addict
    Banned

    Joined:
    Aug 2, 2009
    Posts:
    270
    Referrals:
    0
    Sythe Gold:
    0
    [PHP] Simple search query

    Hello, I'm going to show you simple snippet for doing search query of MYSQL database.

    I'm using MYSQLI on this tutorial.

    Create new file called search.php (name doesn't matter, just make sure that extension is php)

    Let's start with out small HTML part, let's add search format for users so they can enter their search.

    Code:
    <form action="search.php" method="GET">
            <input type="text" name="query" />
            <input type="submit" value="Search" />
        </form>
    
    That's it, let's move on php part.

    To start use of php, add:
    Code:
    <?php
    On same file (search.php) add:
    Code:
    $min_length = 3; //min length of the search
    Like I have commented on that line, that variable is used to define min length of the search.

    Let's move on, add this...

    Code:
     if(strlen($query) >= $min_length){
             
            $query = htmlspecialchars($query); 
    
            $raw_results = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM TABLENAMEHERE
                WHERE (`COLUMNSNAMEHERE` LIKE '%".$query."%')") or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
                 
            
            if(mysqli_num_rows($raw_results) > 0){ // If it find's more than 0 results...
                 
                while($results = mysqli_fetch_array($raw_results)){
    
    echo "<p>".$results['columntodisplayhere']."</p>"; // show's the results..
                 
    
                }
                 
            }
            else{ // If found nothing..
                echo "No results";
            }
             
        }
        else{ // if length of the search is less than defined on variable...
            echo "Minimum length is ".$min_length;
        }
    ?>
    Create file called connect.php
    We are going to create file that is used to include the mysqli connection to database.

    Code:
    <?php
        ($GLOBALS["___mysqli_ston"] = mysqli_connect("localhost",  "USERHERE",  "PASSHERE")) or die("Error connecting to database: ".((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
         
        ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE DBHERE")) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
       
    ?>
    Now you're done. Remember that you need to have your MYSQL database defined on connect.php.
    This tutorial is newbie friendly, but I didn't comment every line.
     
  2. ultimatstore

    ultimatstore Active Member
    Banned

    Joined:
    Apr 16, 2013
    Posts:
    197
    Referrals:
    0
    Sythe Gold:
    0
    [PHP] Simple search query

    thanx
     
  3. RocketPower

    RocketPower WARROCKPS.COM
    Banned

    Joined:
    May 18, 2013
    Posts:
    843
    Referrals:
    0
    Sythe Gold:
    0
    [PHP] Simple search query

    Ty :)
     
  4. Not Cruel Machine

    Not Cruel Machine Newcomer
    Banned

    Joined:
    Jan 6, 2014
    Posts:
    3
    Referrals:
    0
    Sythe Gold:
    0
    [PHP] Simple search query

    Oh dear. Nobody use this if they plan on making anything that anyone will actually use.

    Code:
    $query = htmlspecialchars($query);
    
    You should read into SQL injection techniques and using prepared statements as a safety measure.
     
  5. MineMarkPT

    MineMarkPT Forum Addict
    Banned

    Joined:
    Jun 19, 2012
    Posts:
    477
    Referrals:
    0
    Sythe Gold:
    0
    [PHP] Simple search query

    Doesn't protect against SQL injection, meaning -> Not safe for use unless the creator is the only person that actually uses the website.
     
< How to get the necessary pictures for Reporting a Scammer | CPA + PPD Guide, make $1000+ a month! (Idk If allowed but if not delete this please) >


 
 
Adblock breaks this site