How To Create A Database Search With MySQL & PHP Script

Share:

How To Create A Database Search With MySQL & PHP Script. A website will often make calls to the MySQL database server to display data in it. With PHP scripts, we can create a simple search that will help you and your blog visitors to get the data they need, an example is a list of items and prices.
How To Create A Database Search With MySQL & PHP Script

How To Create A Database Search With MySQL & PHP Script

In this tutorial, we will try to discuss how to create a MySQL database search engine in MySQL database table.

Download now How To Create A Database Search With MySQL & PHP Script

Create MySQL Database Table
For example, create a database name with databasename and book as the table name. Example like :

CREATE TABLE 'book' (
  'id' int(11) NOT NULL auto_increment,
  'title' varchar(300) NOT NULL,
  'author' varchar(100) NOT NULL,
  'price' float(500, 2) NOT NULL,
  PRIMARY KEY  ('id')
) ;

Finish creating a book table, fill in the data with the title, author and price below.

Title Books Author Price
Beginning CSS3 David Powers $25.70
PHP Solutions: Dynamic Web Design Made Easy David Powers $23.65
PHP and MySQL 24-Hour Trainer Andrea Tarr $27.00
Web Design All-in-One For Dummies Sue Jenkins $29.15
HTML and CSS : Design and Build Websites Jon Duckett Jon Duckett $15.94

Create HTML Search Form
The next step is to create a simple search form with html code. If you still do not understand how to create an HTML form, you can learn through tutorials : Create A HTML Form. Back to the discussion, here’s how to create an html search form :
database-search-form.html

<html>
<head>
<title> How To Create A Database Search With MySQL & PHP Script | Tutorial.World.Edu </title>
</head>
<body>
<form action="search-database.php" method="post">
Search: <input type="text" name="search" placeholder=" Search here ... "/>
<input type="submit" value="Submit" />
</form>
<p><a href="http://tutorial.world.edu/web-development/how-to-create-database-search-mysql-php-script/">PHP MySQL Database Search</a> by <a href="http://tutorial.world.edu">Tutorial.World.Edu</a></p>
</body>
</html> 

Create PHP Search Script
In order for the search form can be used to perform a search process in a MySQL database table, then we have to make a php script that will call the data in the table and match it with the keyword being searched.

To connect and call MySQL database table, in this tutorial we use PHP PDO, as an alternative you can also use MySQLi. Here’s the full script of the PHP database search engine :
php-database-search.php

<?php 
//load database connection
    $host = "localhost";
    $user = "root";
    $password = "";
    $database_name = "databasename";
    $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from book where title LIKE '%$search%' OR author LIKE '%$search%'  LIMIT 0 , 10");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
         if (!$query->rowCount() == 0) {
		 		echo "Search found :<br/>";
				echo "<table style=\"font-family:arial;color:#333333;\">";	
                echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Title Books</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Author</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Price</td></tr>";				
            while ($results = $query->fetch()) {
				echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";			
                echo $results['title'];
				echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
                echo $results['author'];
				echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
                echo "$".$results['price'];
				echo "</td></tr>";				
            }
				echo "</table>";		
        } else {
            echo 'Nothing found';
        }
?>

The example result is :
 How To Create A Database Search With MySQL & PHP ScriptDisadvantages of php scripts on these search engines, php scripts are not sensitive to the searched keywords so the results are not so accurate. If you are looking for with the keyword, SQL the search results that have the word such as MySQL, MySQLi and others will also displayed.

Instead, do not practice this tutorial on your web hosting. First, use localhost server. If successful, you can develop and use for purposes of your website.
Thanks for How To Create A Database Search With MySQL & PHP Script

[Tutorial.World.Edu]

Leave a Reply

Your email address will not be published. Required fields are marked *