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.
That's it, let's move on php part.
To start use of php, add:
On same file (search.php) add:
Like I have commented on that line, that variable is used to define min length of the search.
Let's move on, add this...
Create file called connect.php
We are going to create file that is used to include the mysqli connection to database.
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.
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
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.


