Jump to content

[SOLVED] How to display images from a database in search results?


Craigus

Recommended Posts

Hey guru's... ;D

 

I'm very new to the whole php/mysql thing. I've been playing with a database/search as a project and I would like to have an image that is already stored in my database (longblob) to be displayed along with the rest of my search results. I've looks about on the interweb for an example or tutorial with no luck so I'm hoping someone here can point me in the right direction.

 

My search.php

 

<?php

// Displayed if form submitted
if ($searching =="yes")
{
echo "<h2>Search Results</h2><p>";

// No search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Connect to the Database
mysql_connect("localhost", "****_db", "****") or die(mysql_error());
mysql_select_db("****_db") or die(mysql_error());

// Preform filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Search for our search term, in field specified
$data = mysql_query("SELECT * FROM ****_db WHERE upper($field) LIKE'%$find%'");

//Display results
while($result = mysql_fetch_array( $data ))
{
echo $result['species'];
echo "<br>";
echo $result['location'] , " - " , $result['state'];
echo "<br>";
echo $result['date'];
echo "<br>";
echo $result['comments'];
echo "<br>";
echo "<br>";
}

// Counts the number or results
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

// Searched for
echo "<b>Searched For:</b> " .$find;
}
?> 

 

Cheers

Craig.

I think this is the best way to use blob of images in mysql:

 

//getImages.php?ID=17
// This will search the database for an image of id 17 then change the header of the file to output an image

if(isset($_GET['ID']) && !empty($_GET['ID'])) {
    $_GET['ID'] = mysql_real_escape_string(strip_tags($_GET['ID']));

    $connection = mysql_connect("localhost", "****_db", "****") or die(mysql_error());
    mysql_select_db("****_db", $connection) or die(mysql_error());

    $data = mysql_query("SELECT * FROM `images`  WHERE `ID` = '{$_GET['ID']}'");

    $anymatches=mysql_num_rows($data);
    if ($anymatches == 0) {
        exit();
    }

    header("Content-type: image/gif"); // or whatever the mime type is
    print $data['ImageBlob']; // or whatever the blob field is
    exit();
}

 

Now to use it!

 

$imageID = 17;

<p><img src="getImage.php?imageID=<?=$imageID;?>" /></p>

OK, I'm not sure if I made what I wanted to do complety clear.

 

The code above will let me display a single image from my database correct?

 

Each of my entries may or maynot have an image attached, when the results are displayed I would like each result that has an image have that image displayed with the results.

 

For example;

My search page is; http://www.birdingoz.com.au/db/search.php

 

If you search for ROBIN, select species and hit go there are two results. I would like if there is an image attached to the 'image' blob table to have it display next to the corresponding result.

 

Maybe I'm not understanding how the above code can be used though.. If so, my apologies.

 

Craig.

OK, I've got this working. Thanks a bunch for the input.

 

for anyone interested this is what I did;

 

Created - display.php

<?php

$username = "****_db";
$password = "****";
$host = "localhost";
$database = "****_db";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

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

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM bird_db WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;

}

?> 

 

Then added the following to my search.php

//Display results
while($result = mysql_fetch_array( $data ))
{
echo $result['species'];
echo "<br>";
echo $result['location'] , " - " , $result['state'];
echo "<br>";
echo $result['date'];
echo "<br>";
echo $result['comments'];
echo "<br>";
if ($result['id'] > 0)
{
echo "<A HREF=/db/display.php?id=";
echo $result['id'];
echo "><IMG SRC=/db/display.php?id=";
echo $result['id'];
echo " height=200></A><br>";
}
echo "<br>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.