Jump to content

Recommended Posts

Hi all,

 

The problem I am having is when I call an image from a MySQL database, rather than getting the image I get thousands lines of gibberish characters.

�����JFIF��H�H�����C����C��������������������  ���h�� �  !"#1AX��$2Wx���%389BQVw��&6Rav����(47Y���'5CDTUq��ESc����)bu������������������ ���j� �! "1#2A$Q3Bau�%68RTUqw����457CSVWbtv��������&DX����cr����ds��'ef���������?����D�&0��VgʯP�p�0�<�����t��Ym

 

What could cause the problem.? Thanks

Link to comment
https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/
Share on other sites

Thank you for the reply. I already did that but still i am getting the gibberish content of the image.

This is some part of the code

<form method="post" action="admin_home.php">
<tr>          
            <td width="430" align="center"><strong><u>Logo</u></strong><td/>
            <td width="550" align="center"><strong><u>Action</u></strong><td/>        
        <tr/>
      
	<? 
	$query_category="SELECT ImageContent,ImageTitle, Event_Name FROM Logo";
		$result_category = mysql_query($query_category);
		while($row=mysql_fetch_row($result_category)){
	?>
			<tr>
				<td width="430" align="center">
                    	<? 
						header("Content-length: $size");
						header("Content-type: $type");
						header("Content-Disposition: attachment; filename=$name");
						echo "$row[0] - $row[1] - $row[2]" ;
					?><td/>
				<td width="550" align="center">
					<? echo "<a href=\"delete_logo.php?Logo=$row[0]&RaceDist=$row[1]&Event=$row[2]\"><button>Remove Logo</button><a/> ";?><td/>        
			<tr/>
			<?
		}
	?>
    </table>
</form>

You need to use proper html tags on a web page and you cannot output image data directly on a web page - http://www.w3schools.com/html/html_images.asp

 

Each image on a web page requires an <img src="url_that_results_in_an_image" alt=""> tag.

 

The url_that_results_in_an_image in the above must be to a .php file that outputs the header and the image data.

No..... try reading PFMaBismAd's post again.

 

 

image.php

<?php
$id = (int)$_GET['id'];
include("dbconnect.php");
$query = "SELECT ImageContent FROM Logo WHERE id = " . $id;
$results = mysql_query($query);
$data = mysql_fetch_array($results);

header("Content-type: image/jpeg");
echo $data[0];
?>

 

 

test.php


<img src="image.php?id=123" />

I am able to display the image with this code

<?php 

mysql_connect("localhost","xxxxxx","xxxxxx");
mysql_select_db("xxxxxx");
$query_category="SELECT ImageContent FROM Logo";
$result_category = mysql_query($query_category);
while($row=mysql_fetch_row($result_category))
{
header("Content-type: image/jpeg");
echo "$row[0]";
}	
?>

I just found this article... and im going for it, no other choice..

it stores the image to the database and retrieves the image as string..

 

http://www.phpbuilder.com/columns/florian19991014.php3?page=1

http://www.phpbuilder.com/columns/florian19991014.php3?page=2

http://www.phpbuilder.com/columns/florian19991014.php3?page=3

 

hope this solves the entire problem.. :)

I just get the first row's image with this code. Shouldn't it display the images of all the rows?

 

<?php 
mysql_connect("localhost","xxxxxx","xxxxxx");
mysql_select_db("xxxxxxxx");
$query_category="SELECT ImageContent FROM Logo";
$result_category = mysql_query($query_category);
while($row=mysql_fetch_row($result_category))
{
header("Content-type: image/jpeg");
echo "$row[0]" ;
}	
?>

Each image requires a HTML <img src="url_that_results_in_an_image" alt=""> tag on the web page and the url_that_results_in_an_image must output that corresponding single image (header and image data.) Some form of an id number or the image name that is related to each image in your database is usually used on the end of the url -

 

<img src="your_php_code.php?id=1" alt="">
<img src="your_php_code.php?id=2" alt="">
<img src="your_php_code.php?id=3" alt="">

 

Then in your php code that is fetching the image from the database, you use $_GET['id'] to retrieve and output the correct header and image data for that <img tag.

the best way to go about it that i have found is not actually storing the images in the database rather just save the path to the image.  the code i have used looks like this

 

$broad1name2="image/thumbs/thumb_".$image_name

 

this section of the code stores the path to the file into a variable.  the next section stores the variable in the mysql database.

 

$sql="INSERT INTO images_broad (broad1) VALUES ('$broad1name2')";
 $query = mysql_query($sql);

 

to display the images from the database the code i used looks like this

 

<?php
$query = "SELECT broad1 FROM images_broad";
$result=mysql_query($query);

while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<img src="'.$row['broad1'].'"/>';
}

 

to make things a little bit clearer i will show you the code i used to set the table up so the code above makes a little bit more sense.

 

$sql="CREATE TABLE IF NOT EXISTS `images_broad` (

		`id` INT NOT NULL AUTO_INCREMENT,
		`broad1` varchar (250) not null,
		`broad2` varchar(250) not null,
		primary key (id)
	)";	
	mysql_query($sql) or die (mysql_error());

 

hope this sorts out what you was looking for.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.