Jump to content

Recommended Posts

I'm new to php and mysql, but I'm starting to pickup pretty quickly.  I have spent the last day searching and searching for what is wrong with my code.  I have my pictures (car1.jpg, car2.jpg, car3.jpg) uploaded to the 'Picture' field in the 'rent' table as blobs.  Here is my code:

 

<?php
      //Connect To Database
$hostname='';
$username='';
$password='';
$dbname='';
$usertable='rent';
$yourfield = 'Picture';
mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
//select database
mysql_select_db($dbname);
$query = 'SELECT * FROM rent';
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);
mysql_close();
?>

<table border="1" cellspacing="0" cellpadding="10">
<tr>
<th bgcolor="#580201" class="style10">Picture</th>
<th bgcolor="#580201" class="style10">Make</th>
<th bgcolor="#580201" class="style10">Model</th>
<th bgcolor="#580201" class="style10">Category</th>
</tr>

<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"Picture");
$f2=mysql_result($result,$i,"Make");
$f3=mysql_result($result,$i,"Model");
$f4=mysql_result($result,$i,"Category");
?>

<tr>
<td align="center" class="style6"><?php header('Content-type: image/jpg'); echo $f1; ?></td>
<td align="center" class="style6"><?php echo $f2; ?></td>
<td align="center" class="style6"><?php echo $f3; ?></td>
<td align="center" class="style6"><?php echo $f4; ?></td>
</tr>

<?php
$i++;
}
?>

 

The code displays the table exactly how I want with the exception of the pics.  The pics show up as alot of jibberish preceded by a warning about not being able to modify the header.

 

Now I have read that I shouldn't be storing pics on mySQL, but I tried simply storing the image urls and wasn't able to get the pics to display going that route either.

 

I am on the verge of migraines please help.

Link to comment
https://forums.phpfreaks.com/topic/172057-solved-pictures-from-mysql/
Share on other sites

Stop the migraines.

 

Don't store binary data in MySQL. It is a logical failure.

 

Store binary data on the filesystem, store the path to the file in the DB.

 

----

 

Why is it a logical failure?

 

To pull an image from the drive, apache merely awakens, asks for the file the sends it along.

 

 

Storing it in a database requires the following;

 

Apache gets the request and wakes up the PHP parsing engine.

PHP requests the data from MySQL

MySQL Seeks, reads and sends the data to PHP which then passes it along to apache.

 

So, for both sanity and performance purposes.. let the filesystem handle binary data, not the database. There is no reason for it and there are tons of articles floating around about how storing binary data in a DB is a poor choice.

 

Should be very trivial for you to refactor image location/content.

ok, so if I store the files in a filesystem... how do I call them?

 

<img src="<?php echo $f1 ?>">

 

Because I have tried that line of code before with no success unless I need to be storing the entire url in mysql (hhtp://www.blah.com/blah/blah/blah.jpg)  instead of ../blah/blah/blah.jpg

I got it to work.  I'm so happy that I fixed it, but so upset with myself for not catching it sooner.  The reason it was not working is because I had the files saved as car1.JPG and the code said car1.jpg

 

I really could just smash my face against the wall for an hour right now.

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.