Page 1 of 1
MySQL / Images - Issues
Posted: Tue Mar 16, 2010 9:31 am
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
Re: MySQL / Images - Issues
Posted: Tue Mar 16, 2010 12:58 pm
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
Re: MySQL / Images - Issues
Posted: Tue Mar 16, 2010 2:47 pm
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
Re: MySQL / Images - Issues
Posted: Tue Mar 16, 2010 3:07 pm
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.
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 9:15 am
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
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 9:45 am
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
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 10:09 am
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.
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 3:24 pm
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');
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 3:30 pm
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
Re: MySQL / Images - Issues
Posted: Wed Mar 17, 2010 6:36 pm
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)); ?>
Re: MySQL / Images - Issues
Posted: Sat Mar 20, 2010 6:14 pm
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