PHP-MySQL

Searching Images using PHP

To search images using PHP from MySQL Database, follow the examples.

First of all create a table in MySQL database with following fields.

Now, insert some images by clicking on “insert” tab in MySQL.

Now, create a HTML page as following tags.

<html>
<body>
<h1> Image searching.........</h1>
<form name=f1 action="image.php" method="GET">
Enter image Id: <input type=text name=t1>
<input type=submit value="Display"> 
</form>
</body>
</html>

After creating above HTML page, now create PHP config file with following code in it.[save as “config.php”] this file contains database name, user, password etc.

<?php
      $host="localhost";
      $username="root";
      $password="";
      $dbname="mars_db";
      $con=mysql_connect("$host","$username","$password");
      mysql_select_db("$dbname",$con);
?>

Now create PHP file to search stored images in database.
Copy the following code and save as “image.php”.

<?php
$key=$_GET['t1'];
include './config.php';
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($dbname) or die("Can not select the database: ".mysql_error());

$query = mysql_query("SELECT * FROM photo WHERE pid=' ".$key."'");

$row = mysql_fetch_assoc($query);

$contents = $row['content'];

header("Content-type: image/jpg");
echo $contents;
?>

Now, run HTML page as following.

Finally the output looks like this.

That’s all for now!, Thank you for reading!

SKB