• Log in or sign up for an account here: https://www.sythe.org/login

[PHP] Simple search query

Natan

Forum Addict
Banned
Joined
Aug 2, 2009
Sythe Gold
0
Messages
270
Location
Finland
[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.
​
 
[PHP] Simple search query
thanx
 
[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.
 
[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.
 
Runescape Gamblingrunescape gambling
Back
Top