Scubey Posted August 27, 2009 Share Posted August 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172057-solved-pictures-from-mysql/ Share on other sites More sharing options...
awpti Posted August 27, 2009 Share Posted August 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172057-solved-pictures-from-mysql/#findComment-907276 Share on other sites More sharing options...
Scubey Posted August 27, 2009 Author Share Posted August 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/172057-solved-pictures-from-mysql/#findComment-907519 Share on other sites More sharing options...
Scubey Posted August 27, 2009 Author Share Posted August 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172057-solved-pictures-from-mysql/#findComment-907784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.