MySQL / Images - Issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

MySQL / Images - Issues

Post by Metal-X-Man »

Here's my issue: I need to call an image from MySQL. Problem is, I'm trying to structure the output in HTML (or a DIV). You can't do this without getting "Header" errors.

Is there another way to handle this? And still keep calling images from MySQL?


Metal-X-Man
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: MySQL / Images - Issues

Post by AbraCadaver »

You need to display the page as an HTML page without the image header and then use an <img> tag with the src set to a PHP file that grabs the image from the db and outputs the header and image. See here: viewtopic.php?f=1&t=110171&p=582727#p582727
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

I see now that this was a question you answered a while ago. Thank you for your patience!

Rookie Question: On the show_image.php, will I need to do a database connect and close on this page? I already have one on the page that calls the show_image.php.

Metal-X-Man
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: MySQL / Images - Issues

Post by John Cartwright »

Metal-X-Man wrote:On the show_image.php, will I need to do a database connect and close on this page?
Yes. They are completely separate HTTP requests.
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

Here's my code. I'm still having issues with the show_images.php

The display page with table:
-------------------------------

Code: Select all

[size=85][color=#0000FF]<?php require_once('../Connections/myDB.php'); ?>
<?php
 
mysql_select_db($database_ myDB, $ myDB);
$query_rs1 = "SELECT * FROM news ORDER BY id DESC";
$rs1 = mysql_query($query_rs1, $myDB) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1); ?>
            
 
<?php do { ?>
 <table width="426" border="0" cellpadding="5" cellspacing="2">
 <tr>
        <td colspan="5" align="left" valign="top" bgcolor="#999999"><?php echo $row_rs1['news_title']; ?></td>
  </tr>
  <tr>
        <td width="150" align="center" valign="middle" bgcolor="#666666"><?php echo '<img src="show_image.php?id='.$row_rs1['id'].'">';?></td>
        <td colspan="4" align="left" valign="top" bgcolor="#CCCCCC" style="color:#FFF;"><?php echo $row_rs1['news_text']; ?></td>
  </tr>
  <tr></tr>
  </table>
  <?php } while ($row_rs1 = mysql_fetch_assoc($rs1)); ?> [/color] [/size]
And here's the show_image.php:
---------------------------------

Code: Select all

<?php require_once('../Connections/myDB.php');?>
 
<?php 
 
mysql_select_db($database_myDB, $myDB);
$query_rs1 = "SELECT'news_image_data' FROM news WHERE 'id' = " . (int)$_GET['id'];
$rs1 = mysql_query($query_rs1, $myDB) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
header('Content-type: image/jpg');
echo $row_rs1['news_image_data'];
 
?>
[/size][/color]

I worked on this for hours last night and still didn't get it working. Any glaring issues that you guys see?

Metal-X-Man
Last edited by Metal-X-Man on Wed Mar 17, 2010 10:11 am, edited 2 times in total.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: MySQL / Images - Issues

Post by mikosiko »

Metal-X-Man wrote:Here's my code. I'm still having issues with the show_images.php

The display page with table:
-------------------------------

Code: Select all

 
[size=85][color=#0000FF]<?php require_once('../Connections/myDB.php'); ?>
<?php
 
mysql_select_db($database_ myDB, $ myDB);
$query_rs1 = "SELECT * FROM news ORDER BY id DESC";
$rs1 = mysql_query($query_rs1, $myDB) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1); ?>
            
 
<?php do { ?>
 <table width="426" border="0" cellpadding="5" cellspacing="2">
 <tr>
        <td colspan="5" align="left" valign="top" bgcolor="#999999"><?php echo $row_rs1['news_title']; ?></td>
  </tr>
  <tr>
        <td width="150" align="center" valign="middle" bgcolor="#666666"><?php echo '<img src="images.php?id='.$row_rs1['id'].'">';?></td>
        <td colspan="4" align="left" valign="top" bgcolor="#CCCCCC" style="color:#FFF;"><?php echo $row_rs1['news_text']; ?></td>
  </tr>
  <tr></tr>
  </table>
  <?php } while ($row_rs1 = mysql_fetch_assoc($rs1)); ?> [/color] [/size]
 
 
And here's the [color=#FF0000]show_image.php:[/color]
---------------------------------
[color=#0000FF] [size=85]<?php require_once('../Connections/myDB.php');?>
 
<?php 
 
mysql_select_db($database_myDB, $myDB);
$query_rs1 = "SELECT'news_image_data' FROM news WHERE 'id' = " . (int)$_GET['id'];
$rs1 = mysql_query($query_rs1, $myDB) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
header('Content-type: image/jpg');
echo $row_rs1['news_image_data'];
 
?>[/size][/color]
 
I worked on this for hours last night and still didn't get it working. Any glaring issues that you guys see?

Metal-X-Man
First... use the tag code to enclose your codding... better for clarity....

now... where are you calling "show_image.php".... I don't see it... look up the text in red
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

Sorry, learning the board. I'll use the code post next time.

Rather than calling it "show_images.php" I called it "images.php". I fixed my original post now to reflect "show_image.php". so that we keep this post using the same names.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: MySQL / Images - Issues

Post by AbraCadaver »

While developing you need to put this at the top of files that contain PHP:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

I'll try that tonight. The "show_image" page doesn't call the image from the database. I have a list of tests that I'll run tonight and see what the issue is. I'll call text rather than the blob to test the connection.

Metal-X-Man
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

Issue solved! Thanks for the help! It turns out is was a database connection issue on the show_images.php page. I'll re-post the correct code for other people!


show_image.php

Code: Select all

<?php require_once('../Connections/myDB.php'); ?>
<?php
 
mysql_select_db($database_mrDB, $myDB);
$query_rs2 = ("SELECT news_image_data FROM news WHERE id = "  . (int)$_GET['id']);
$rs2 = mysql_query($query_rs2, $myDB) or die(mysql_error());
$row_rs2 = mysql_fetch_assoc($rs2);
$totalRows_rs2 = mysql_num_rows($rs2);
?>
<?php 
// Notice I don't have the image type stored so I had to hard code this jpg
// see AbraCadaver original post for the dynamic image type
 
header('Content-type: image/jpg');
echo $row_rs2['news_image_data'];
?>
page displaying the image

Code: Select all

<?php require_once('../Connections/myDB.php'); ?>
<?php
 
mysql_select_db($database_myDB, $myDB);
$query_rs1 = "SELECT * FROM news ORDER BY id DESC";
$rs1 = mysql_query($query_rs1, $myDB) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1); ?>
            <?php do { ?>
                <table width="426" border="0" cellpadding="5" cellspacing="2">
                <tr>
                    <td colspan="5" align="left" valign="top" bgcolor="#999999"><?php echo $row_rs1['news_title']; ?></td>
                </tr>
                <tr>
                    <td width="150" align="center" valign="middle" bgcolor="#666666"><?php echo '<img src="show_images.php?id='.$row_rs1['id'].'" height="150">';?></td>
                    <td colspan="4" align="left" valign="top" bgcolor="#CCCCCC" style="color:#FFF;"><?php echo $row_rs1['news_text']; ?></td>
                </tr>
                <tr></tr>
                </table>
            <?php } while ($row_rs1 = mysql_fetch_assoc($rs1)); ?>
Last edited by Metal-X-Man on Sat Mar 20, 2010 6:14 pm, edited 1 time in total.
User avatar
Metal-X-Man
Forum Newbie
Posts: 13
Joined: Tue Mar 16, 2010 9:25 am
Location: Columbus, Ohio

Re: MySQL / Images - Issues

Post by Metal-X-Man »

One last question:

I want the user to be able to click the image and get a large view. Because I limit the width of the image to 150px when calling show_images.php, by calling show_images.php again, I should be able to get a large view of the image.

I need help with the code. I'll try and do something like this:

Code: Select all

[b][color=#BF0000]<a href="<?php show_image.php?id='$row_rs1['id'].' " ?> target="_blank">[/color][/b] <?php echo '<img src="show_images.php?id='.$row_rs1['id'].'" height="150">';?>[color=#BF0000][b]</a>[/b][/color]
I can't seem to get the href working. The problem is in the href. What looks bad here?

Metal-X-Man
Post Reply