Forum Moderators: coopster

Message Too Old, No Replies

Image Show Php+mysql

IE & Firefox get garbage from jpeg

         

sseral

3:11 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



Hello friends,

Displaying jpeg images from MySQL database one at a time is no problem.I could not find a way of displaying few of them in a table format though. All the sample scripts I tried failed. Her is my displaying script. Can anyone tell me where do I go wrong please. I will be grateful.

/* lines to connect to my database here */

$sql = "SELECT * FROM emlak_detay";
$result = mysql_query($sql, $emlak) or die(mysql_error());
$rows = mysql_num_rows($result);

echo "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n";
echo " <tr>\n";
echo " <td>Filename</td>\n";
echo " <td>Filetype</td>\n";
echo " <td>Filesize</td>\n";
echo " <td>blobdata</td>\n";
echo " </tr>\n";

for ($i = 0; $i < $rows; $i++) {
$data = mysql_fetch_object($result);
echo " <tr>\n";
echo " <td>$data->file_name</td>\n";
echo " <td>$data->file_type</td>\n";
echo " <td>$data->file_size</td>\n";
//header("Content-type:$data->file_type");
echo " <td>$data->bin_data</td>\n";
echo " <td>";
echo "</td>\n";
echo " </tr>\n";
}

Whether I uncomment the header line or not, I get garbage on the screen. The rest of the info comes from emlak database is perfect.

Birdman

3:55 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you need to do is create a stand-alone php script that outputs the images. Then, in your other script, call the script from the image tags.

You may need to adjust some stuff, but here is an example:

// no need to select bin_data here
$sql = "SELECT id, file_name, file_type, file_size FROM emlak_detay";
$result = mysql_query($sql, $emlak) or die(mysql_error());

<CUT>

while($data = mysql_fetch_object($result)){
echo " <tr>\n";
echo " <td>$data->file_name</td>\n";
echo " <td>$data->file_type</td>\n";
echo " <td>$data->file_size</td>\n";
echo ' <td><img src="image.php?id='.$data->id.'" alt="" /></td>\n';
echo " </tr>\n";
}

The file image.php:

<?php
if(!isset($_GET['id']) ) { exit; }

<CONNECT TO MYSQL>
$sql = "SELECT bin_data, file_type FROM emlak_detay where id = " . $_GET['id'] . " limit 1";
$result = mysql_query($sql, $emlak) or die(mysql_error());
$data = mysql_fetch_object($result);

header("Content-type: " . $data->file_type );
print $data->bin_data;
exit;
?>

sseral

7:52 am on Jan 16, 2005 (gmt 0)

10+ Year Member



Hello Birdman,

Thank you for your prompt reply. Your solution does not solve my problem though. Because I want to build a page that will be formed dynamically depending on my data in mysql which includes related images & text on every row of this table. I can be more specific if you want to follow my case. Thank you anyway. I appeciate your help.

Birdman

12:02 pm on Jan 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because I want to build a page that will be formed dynamically depending on my data in mysql

The example I gave does exactly that!

You cannot output images in the way you are trying to do in your first post. All images will require another HTTP GET. You see, once the text/html content header is sent, that's what the browser expects. Not images.

Maybe you should try the example. I have done it, so I know it works.