Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Problem with pulling image from database.
New Posts  All Forums:Forum Nav:

Problem with pulling image from database.

post #1 of 17
Thread Starter 
Hey Guys.

I'm creating a website to display a whole bunch of miniatures which my granddad painted during his life time. Approximately 2,000+ of the American Civil War.

I had a question about pulling an image from a Database. I have managed to get information, such as text from the database, but I have been unable to successfully display the image.

Here is the site. You can search through the different troop types, I only have two tests in there. Both you will see has the classic

"Warning: Cannot modify header information - headers already sent by (output started at /homepages/28/d413246400/htdocs/Test/head.php:10) in /homepages/28/d413246400/htdocs/Test/troops.php on line 32
8124774.jpeg"

The code is here:
Code:
<?php include 'head.php'?>

   <div id="maincontent">
   <br>
   <div class="article">
    <?php 
                $t1 = $_GET['t1'];
                $t2 = $_GET['t2'];
                $a1 = "Union";
                $a2 = "Confederate";
                if ($t1!=$a1 AND $t1!=$a2){
                        echo "<p>This army doesn't exist.</p>";
                }
                else{
                        echo "<h1>$t1</h1>";
                        $b1 = "Command";
                        $b2 = "Infantry";
                        $b3 = "Cavalry";
                        $b4 = "Artillery";
                        if($b1!=$t2 AND $b2!=$t2 AND $b3!=$t2 AND $b4!=$t2 AND $t2="")
                                echo "<p>This troop type doesn't exist.</p>";
                        else {
                                echo "<h2>$t2</h2></div>";
                                if($t2=="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1'";
                                if($t2!="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1' AND `type` = '$t2'";
                                        
                                $troop = mysql_query($query);
                                while($row = mysql_fetch_array($troop)){
                                        echo '<div class="troop"><h3>'.$row['name'].'</h3><p>'.$row['description'].'</p>';
                                        header("Content-type: image/jpeg");
                                        echo $row['image'];
                                }
                        }
                }
                
        ?>
    </div>
   </div>
</div>
</body>
</html>
post #2 of 17
You can't send headers with header() after you've already output contents. What you need to use is output buffering - add ob_start() at the top of your script, and ob_end_flush() at the end. smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #3 of 17
Thread Starter 
Ok, I just updated it as you said, but got the same error.
Code:
<?php include 'head.php'?>

   <div id="maincontent">
   <br>
   <div class="article">
    <?php 
                ob_start();
                $t1 = $_GET['t1'];
                $t2 = $_GET['t2'];
                $a1 = "Union";
                $a2 = "Confederate";
                if ($t1!=$a1 AND $t1!=$a2){
                        echo "<p>This army doesn't exist.</p>";
                }
                else{
                        echo "<h1>$t1</h1>";
                        $b1 = "Command";
                        $b2 = "Infantry";
                        $b3 = "Cavalry";
                        $b4 = "Artillery";
                        if($b1!=$t2 AND $b2!=$t2 AND $b3!=$t2 AND $b4!=$t2 AND $t2="")
                                echo "<p>This troop type doesn't exist.</p>";
                        else {
                                echo "<h2>$t2</h2></div>";
                                if($t2=="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1'";
                                if($t2!="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1' AND `type` = '$t2'";
                                        
                                $troop = mysql_query($query);
                                while($row = mysql_fetch_array($troop)){
                                        echo '<div class="troop"><h3>'.$row['name'].'</h3><p>'.$row['description'].'</p>';
                                        header("Content-type: image/jpeg");
                                        echo $row['image'];
                                }
                        }
                }
                ob_end_flush();
        ?>
    </div>
   </div>
</div>
</body>
</html>

And how you doing mate?
post #4 of 17
Quote:
Originally Posted by RadMabbit View Post

Ok, I just updated it as you said, but got the same error.

--snip--

And how you doing mate?

When I say start of the script for ob_start(), I literally mean the first line in the document smile.gif Same for ob_end_flush() - it needs to be the last line of the document so that all output is buffered.
And yeah, pretty good buddy!
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #5 of 17
Thread Starter 
Lol ok, I did that like this. Check this page now...http://s413246417.onlinehome.us/troops.php?t1=Union&t2=Infantry
Code:
<?php ob_start(); include 'head.php'?>

   <div id="maincontent">
   <br>
   <div class="article">
    <?php 
                $t1 = $_GET['t1'];
                $t2 = $_GET['t2'];
                $a1 = "Union";
                $a2 = "Confederate";
                if ($t1!=$a1 AND $t1!=$a2){
                        echo "<p>This army doesn't exist.</p>";
                }
                else{
                        echo "<h1>$t1</h1>";
                        $b1 = "Command";
                        $b2 = "Infantry";
                        $b3 = "Cavalry";
                        $b4 = "Artillery";
                        if($b1!=$t2 AND $b2!=$t2 AND $b3!=$t2 AND $b4!=$t2 AND $t2="")
                                echo "<p>This troop type doesn't exist.</p>";
                        else {
                                echo "<h2>$t2</h2></div>";
                                if($t2=="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1'";
                                if($t2!="")
                                        $query = "SELECT * FROM `acwmUK` WHERE `team` = '$t1' AND `type` = '$t2'";
                                        
                                $troop = mysql_query($query);
                                while($row = mysql_fetch_array($troop)){
                                        echo '<div class="troop"><h3>'.$row['name'].'</h3><p>'.$row['description'].'</p>';
                                        header("Content-type: image/jpeg");
                                        echo $row['image'];
                                }
                        }
                }
        ?>
    </div>
   </div>
</div>
</body>
</html>
<?php ob_end_flush(); ?>
post #6 of 17
Oh yeah, that's right - if you're going to output an image like that, you need to output only an image... you can't send output like normal HTML etc, and then halfway decide to switch content type and start outputting an image. The browser literally goes... um... OK mate! tongue.gif

What you should do is create a separate script that takes a parameter via URL (perhaps an image ID), which then fetches the image contents from the DB and outputs it like so:

showImage.php
Code:
<?php
/* mysql query here, etc */
@header( "Content-type: image/jpeg" );
echo $row['image'];

Then in your main page you can do this:
Code:
<img src="showImage.php?id=image_id" />

smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #7 of 17
Thread Starter 
Ahhhh ok that makes sense! I have it down pretty much I think.

My get image.php file.
Code:
<?php

mysql_connect('db449078161.db.1and1.com','dbo449078161', 'campbell') or die(mysql_error());
mysql_select_db('db449078161') or die(mysql_error());

$id = $_GET['id'];
$imgquery = "SELECT * FROM `acwmUK` WHERE `id` = $id";

header("Content-type: image/jpeg");
echo $imgquery['image'];

?>

Then my bit of code on my main page that calls it...
Code:
$troop = mysql_query($query);
while($row = mysql_fetch_array($troop)){
   $imageid=$row['id'];
   echo '<div class="troop"><h3>'.$row['name'].'</h3><p>'.$row['description'].'</p>';
   echo '<img src="getimage.php?id='.$imageid.' />';
                                }

The only thing that isnt working is displaying the image. Must of missed something obvious. The ID is being picked up ok!

Adam
post #8 of 17
Are the bytes of the image literally being stored in the DB?
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #9 of 17
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

Are the bytes of the image literally being stored in the DB?

The image is stored as a blob in the MySQL database, with a binary attribute.

Then I also store the image name in a separate area. I can get that to display. I am just struggling getting the image to show.
post #10 of 17
You forgot to perform a query in getimage.php to get the image.

Don't forget to validate user input. Your code is vulnerable to SQL injections. A possible way to fix it:
Code:
$id = preg_replace('/[^0-9]/', '', $_GET['id']);
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Problem with pulling image from database.