Home ยป Php ยป how to display image from database? – PHP

how to display image from database? – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

i am doing a site for appliance center..
i need to display all the products that the company offers, but my problem is that i cant display ALL the images in my database.. the first entry on the database is the only one that displays..
i am using mysql as database

here’s the code.. tell me what’s my error and pls. kindly edit it..

**imgdata = it is where i store the image.. longblob is the data type


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$gotten = @mysql_query("select imgdata from pix");
header("Content-type: image/jpeg");
while ($row = mysql_fetch_array($gotten))
{
print $row['imgdata'];
}
mysql_free_result($gotten);
?>

A(Answer):

hi
change in the mysql_fetch_array()


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$gotten = @mysql_query("select imgdata from pix");
header("Content-type: image/jpeg"); while ($row = mysql_fetch_array($gotten,MYSQL_ASSOC))

A(Answer):

Hi,
the problem here is that you are only able to use the content disposition to send out one file.
As each blob stores a different image you will need another page thats loops through the DB, and displays the content disposition page.

Steps to achieve this….
Firstly change your select query to be based on the ID in the table, and get that ID from the query string ($_GET[‘ID’]) or something like that.

Secondly make another page and in that page loop through the results from the table printing out an image with the content disposition page and an ID in the query string.

I can go in to more detail if you like. let me know

A(Answer):

Hi,
the problem here is that you are only able to use the content disposition to send out one file.
As each blob stores a different image you will need another page thats loops through the DB, and displays the content disposition page.

Steps to achieve this….
Firstly change your select query to be based on the ID in the table, and get that ID from the query string ($_GET[‘ID’]) or something like that.

Secondly make another page and in that page loop through the results from the table printing out an image with the content disposition page and an ID in the query string.

I can go in to more detail if you like. let me know

yes, please help me..
and can you show me the code?
im really new with php and im having a hard time..
thanks for the reply..
very much appreciated

A(Answer):

hi
change in the mysql_fetch_array()


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$gotten = @mysql_query("select imgdata from pix");
header("Content-type: image/jpeg"); while ($row = mysql_fetch_array($gotten,MYSQL_ASSOC))

hi!! thanks for the reply..
very much appreciated..
i’ll try this tomorrow and will inform you about the result..
thanks again!!

A(Answer):

So you need two pages.

The first page uses content disposition to output an image given an ID in the quesry string just like you have already only modify the query to select an image based on the query string. Like this…..

Call this page pix.php…

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
if (IsSet($_GET['pixID'])){
$gotten = @mysql_query("select imgdata from pix where pixID = ".$_GET['pixID']);
header("Content-type: image/jpeg");
while ($row = mysql_fetch_array($gotten))
{
print $row['imgdata'];
}
mysql_free_result($gotten);
}
?>

the other page will have a list of img tags with the source pointing to a variation of pix.php and a query string value.

call this page list.php

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
<?php
$i++;
}
?>

Now put these into the same folder and it should work. i haven’t tested you may need to debug slightly

A(Answer):

So you need two pages.

The first page uses content disposition to output an image given an ID in the quesry string just like you have already only modify the query to select an image based on the query string. Like this…..

Call this page pix.php…

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
if (IsSet($_GET['pixID'])){
$gotten = @mysql_query("select imgdata from pix where pixID = ".$_GET['pixID']);
header("Content-type: image/jpeg");
while ($row = mysql_fetch_array($gotten))
{
print $row['imgdata'];
}
mysql_free_result($gotten);
}
?>

the other page will have a list of img tags with the source pointing to a variation of pix.php and a query string value.

call this page list.php

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
<?php
$i++;
}
?>

Now put these into the same folder and it should work. i haven’t tested you may need to debug slightly

hey, thank you very much!! it works!!
i hope i can ask you again for further problems.. ๐Ÿ™‚
thank you..
thank you..
thank you!!

A(Answer):

hi,
absolutly no problem. i love to help. But make sure you ask in the forums so every one can learn.

A(Answer):

hi,
absolutly no problem. i love to help. But make sure you ask in the forums so every one can learn.

i have another problem..
i also want to display the corresponding title of the image..
"title" is the field that handles the title of the image in my table

A(Answer):

i have another problem..
i also want to display the corresponding title of the image..
"title" is the field that handles the title of the image in my table

Hi again…

Something like this….. for the page which displays all the images


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<h1><?php echo mysql_result($rsPix,$i,"pixTitle"); ?></h1>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
<?php
$i++;
}
?>

A(Answer):

Hi again…

Something like this….. for the page which displays all the images


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<h1><?php echo mysql_result($rsPix,$i,"pixTitle"); ?></h1>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
<?php
$i++;
}
?>

hello!! thanks for replying again..
you’re great.. it worked!!
mmm.. can i ask you again?
how can i display all the images in a table of 3 columns while the rows will depend on how many images is in my database

ex:
total # of images: 25
so, there will be 3 columns and 9 rows (the last row has only 1 image)

thanks again for the reply!!

A(Answer):

Hi,
I appreciate your gratitude.

I don’t get that last question fully.
What is the table structure for this new table (you can give me the create query if you like). I can try work it out or give you a proper solution, but i need more info this time…

๐Ÿ™‚

A(Answer):

Hi,
I appreciate your gratitude.

I don’t get that last question fully.
What is the table structure for this new table (you can give me the create query if you like). I can try work it out or give you a proper solution, but i need more info this time…

๐Ÿ™‚

ah ok, i’ll explain it again..
previously, you’ve given me the code for displaying all the images in my database.. when i tried it, it looks like this ( # for example is the image)
# # # # # # # #

i tried putting the code inside a table.
here’s the code:


<table>
<tbody>
<?
if($colCtr % $cols == 0)
{
?>
<tr>
<td><img src="pix.php?pid=<?php echo mysql_result($rsPix,$i,"pid"); ?>"/></td>
</tr>
<? $colCtr++;
}
?>
</tbody>
</table>
but the output looks like this:
#
#
#
#
#
#
#
#

what i want is to be displayed it in 3 columns while the rows will depend on the number of images in my database
ex: total number of images is 7


display:
# # #
# # #
#

ex: total number of images is 11


display:
# # #
# # #
# # #
# #

ex: total number of images is 6


display:
# # #
# # #

please help me again..
and thanks for the reply..
thank you very much..

A(Answer):

Hi, again!

Try using the div solution…
(the output code will be alot cleaner).
If you definately need tables i have another solution. but i’m not proud of that code… (very messy)

Div solution….

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<div>
<?php
while($i < $numRows){
?>
<div style="width:33%: float:left">
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</div>
<?php
$i++;
}
?>
</div>

Table solution…..

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<table>
<tr>
<?php
while($i < $numRows){
?>
<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</td>
<?php
if ($i%3 == 0){
?>
</tr><tr>
<?php
}
?>
<?php
$i++;
}
?>
<?php
if ($numRows%3 > 0){
?>
<td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
<?php
}
?>
</tr>
</table>

Again i must say the code may need a spot of debugging.

A(Answer):

Hi, again!

Try using the div solution…
(the output code will be alot cleaner).
If you definately need tables i have another solution. but i’m not proud of that code… (very messy)

Div solution….

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<div>
<?php
while($i < $numRows){
?>
<div style="width:33%: float:left">
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</div>
<?php
$i++;
}
?>
</div>

Table solution…..

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<table>
<tr>
<?php
while($i < $numRows){
?>
<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</td>
<?php
if ($i%3 == 0){
?>
</tr><tr>
<?php
}
?>
<?php
$i++;
}
?>
<?php
if ($numRows%3 > 0){
?>
<td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
<?php
}
?>
</tr>
</table>

Again i must say the code may need a spot of debugging.

hi.. i’ve tried the codes you’ve given me.. the first one still displays the images like this:


#
#
#
#
#

the 2nd one is the one that i need but there’s something wrong, the first row has only 1 image but the succeeding rows are fine.. it displays like this:


#
# # #
# # #
# # #

where’s the problem with the code?
and where can i put this code

<td><center><?php echo mysql_result($rsPix,$i,"title"); ?></center></td>

so that the image has a title under it?
thanks again for the reply..

A(Answer):

Hi again mate,

First we’ll sort out the table then we’ll stick in the title….

i think i spoted the error with my code….

try this…

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<table>
<tr>
<?php
while($i < $numRows){
?>
<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</td>
<?php
$i++;
if ($i%3 == 0){
?>
</tr><tr>
<?php
}
?>
<?php
}
?>
<?php
if ($numRows%3 > 0){
?>
<td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
<?php
}
?>
</tr>
</table>

i’ve moved the increment before the if statement.

A(Answer):

Hi again mate,

First we’ll sort out the table then we’ll stick in the title….

i think i spoted the error with my code….

try this…

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root",""))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$strSQL = "select * from pix";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
?>
<table>
<tr>
<?php
while($i < $numRows){
?>
<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
</td>
<?php
$i++;
if ($i%3 == 0){
?>
</tr><tr>
<?php
}
?>
<?php
}
?>
<?php
if ($numRows%3 > 0){
?>
<td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
<?php
}
?>
</tr>
</table>

i’ve moved the increment before the if statement.

hello again..
i’ve tried the code, and it works!!
yehey!!.. thanks again!!

so, where will i have to put the code for the title of the image?
i want it to be under the image

ex:

# # #
image1 image2 image3

thanks again for the reply!!
thanks..

A(Answer):

Hi again,
I’m getting excited its almost done!!!

can you not line break and place it under the photo? like this…

<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/><br/>
<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>"/>
</td>

If this will do excellent, and if not i do have another solution but thats gonna take a while to code out.

A(Answer):

Hi again,
I’m getting excited its almost done!!!

can you not line break and place it under the photo? like this…

<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/><br/>
<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>"/>
</td>

If this will do excellent, and if not i do have another solution but thats gonna take a while to code out.

hello again, hey thanks for the help..
really, really thanks..
but im still far from being done.. hehe..
i still have to make the images serve as a link to their features..
thank you very BIG!!

A(Answer):

No problems mate, i like helping! (keeps me sane)

A(Answer):

No problems mate, i like helping! (keeps me sane)

hi.. im here again.. been busy with our thesis.. hehe..
i have a question again.. you have already told me how to display the images and its’ corresponding titles..
i am now trying to make the image’s title to be a link for the image’s features.. but i really cant do it..

ex:

# # #
pic1 pic2 pic3

pic1.php will be responsible for displaying the features of pic1
pic2.php will be responsible for displaying the features of pic2 and so on..
i have tried adding codes to the codes you’ve given me but i know its not correct.. hehe.. im stuck.. here’s the code:


<td>
<center><img src="pix.php?pid=<?php echo mysql_result($rsPix,$i,"pid"); ?>"/></center><br/>
<center><a href = "<?php echo mysql_result($rsPix,$i,"title") . \".php\"; ?>"><?php echo mysql_result($rsPix,$i,"title"); ?></a></center>
</td>

A(Answer):

Hi again mate,

<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/><br/>
<a href="<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>.php"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>
</td>

That will print the links as per your specification. BUT, don’t you think it would be better to have one dynamic page that changes content depending on and ID that you can append to the querystring?

Are you storing the details of the images on the DB? or is the pages hardcoded with content?

A(Answer):

Hi again mate,

<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/><br/>
<a href="<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>.php"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>
</td>

That will print the links as per your specification. BUT, don’t you think it would be better to have one dynamic page that changes content depending on and ID that you can append to the querystring?

Are you storing the details of the images on the DB? or is the pages hardcoded with content?

the code that you’ve given is working..
thanks again..
but can you tell me more about that one dynamic page.. guess that’s easier..

A(Answer):

Hi again mate,

<td>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/><br/>
<a href="<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>.php"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>
</td>

That will print the links as per your specification. BUT, don’t you think it would be better to have one dynamic page that changes content depending on and ID that you can append to the querystring?

Are you storing the details of the images on the DB? or is the pages hardcoded with content?

i think i understand what you’re telling me.. but, am i going to use href for that dynamic page? if yes, what will handle the "id" of the image?,, or maybe i just really dont get the coding process.. but i understand the logic of having a dynamic page (less coding instead of using my idea) hehe.. help again..

A(Answer):

Hi again,

For this you will need two pages…..

One the first page print out this href in the loop i gave you previously…

<a href="<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>

Then on the other "dynaimc page" do something like this….

<?php
$pixID = $_GET['pixID'];
//A bit of DB logic here to get the record from the DB using $pixID
$strSQL = "SELECT * FROM tblPix";
$strSQL .= " WHERE pixID = $pixID";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;
if ($num < 0){
$title = mysql_result($rsPIX,$i,"pixTitle");
$description = mysql_result($rsPIX,$i,"pixDescription");
}
//Now you can print out the two variables there in the page containing the Title and Description.
//I need to make my normal point as this code will need some fixing as i again haven't tested, its ust to point you in the write direction!
?>

I hope that makes sense

A(Answer):

Hi again,

For this you will need two pages…..

One the first page print out this href in the loop i gave you previously…

<a href="<?php echo mysql_result($rsPix,$i,"pixTitle"); ?>.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>

Then on the other "dynaimc page" do something like this….

<?php
$pixID = $_GET['pixID'];
//A bit of DB logic here to get the record from the DB using $pixID
$strSQL = "SELECT * FROM tblPix";
$strSQL .= " WHERE pixID = $pixID";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;
if ($num < 0){
$title = mysql_result($rsPIX,$i,"pixTitle");
$description = mysql_result($rsPIX,$i,"pixDescription");
}
//Now you can print out the two variables there in the page containing the Title and Description.
//I need to make my normal point as this code will need some fixing as i again haven't tested, its ust to point you in the write direction!
?>

I hope that makes sense

i have put the 1st code on my "productDisplay.php",
how about the 2nd one? is it another file? if yes, what file name should i name it?

A(Answer):

i have put the 1st code on my "productDisplay.php",
how about the 2nd one? is it another file? if yes, what file name should i name it?

hi, im sorry.. i have already done it (i get stupid sometimes.. hehe).. just edited some of your codes..
question again..
why is it that when i display the "feature" of the image, it is not displayed properly.. i mean, it looks like this in my database:

* World’s Highest Resolution Full HD Panel (1920 x 1080 pixels)
* New Sophisticated Piano Black Finish Design
* Four-Wavelength Backlight System to deliver natural, pure red
* Viewing angles: 1760 H/V

but it looks like this when it was shown..

* World’s Highest Resolution Full HD Panel (1920 x 1080 pixels) * New Sophisticated Piano Black Finish Design * Four-Wavelength Backlight System to deliver natural, pure red * Viewing angles: 1760 H/V

i also need to display the image again that i choose to view the feature..
something like:

*title*
*image*
*feature*

i can display the title & the feature.. the image is my problem..

thanks again for the reply..

A(Answer):

Oops, i didn’t proof read my code….

Letme try that one again!

on the page that lists the images (name doen’t matter) put this….

<a href="imageDetails.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>

Then on the other "dynaimc page" (in my instance called imageDetails.php) do something like this….

<?php
$pixID = $_GET['pixID'];
//A bit of DB logic here to get the record from the DB using $pixID
$strSQL = "SELECT * FROM tblPix";
$strSQL .= " WHERE pixID = $pixID";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;
if ($num < 0){
$title = mysql_result($rsPIX,$i,"pixTitle");
$description = mysql_result($rsPIX,$i,"pixDescription");
}
//Now you can print out the two variables there in the page containing the Title and Description.
//I need to make my normal point as this code will need some fixing as i again haven't tested, its ust to point you in the write direction!
?>

A(Answer):

Oops, i didn’t proof read my code….

Letme try that one again!

on the page that lists the images (name doen’t matter) put this….

<a href="imageDetails.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>View details for <?php echo mysql_result($rsPix,$i,"pixTitle"); ?></a>

Then on the other "dynaimc page" (in my instance called imageDetails.php) do something like this….

Code: ( text )
<?php
$pixID = $_GET[‘pixID’];
//A bit of DB logic here to get the record from the DB using $pixID
$strSQL = "SELECT * FROM tblPix";
$strSQL .= " WHERE pixID = $pixID";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;

if ($num < 0){
$title = mysql_result($rsPIX,$i,"pixTitle");
$description = mysql_result($rsPIX,$i,"pixDescription");
}
//Now you can print out the two variables there in the page containing the Title and Description.
//I need to make my normal point as this code will need some fixing as i again haven’t tested, its ust to point you in the write direction!
?>

hi.. sorry for the late reply..
i had already solved this, and i had same changes with what you did.. (",)
have you read my post above your reply?
that’s my latest problem..
hehe..
thanks again..

A(Answer):

okies, i never read that.

can you post some mark up this time.

Also your table definition would help

A(Answer):

okies, i never read that.

can you post some mark up this time.

Also your table definition would help

hi!!
what mark up?
my table name is "product"
the fields inside are:

pid (int) > product’s id
title (text) > name of the product
imgdata (longlob) > the image
feature (text) > the product’s feature.. this is also my problem when displaying.. like what i’ve mention above..

A(Answer):

Hi again mate,

I think i know the problem…

replace the line that you have printing out the string with a piece of code that swaps carriage returns with <br/>’s

something like this…..

<?php echo nl2br(mysql_result($rsPix,$i,"feature")); ?>

BTW, markup is what you get when you view source in the browser after the PHP is run

A(Answer):

Hi again mate,

I think i know the problem…

replace the line that you have printing out the string with a piece of code that swaps carriage returns with <br/>’s

something like this…..

<?php echo nl2br(mysql_result($rsPix,$i,"feature")); ?>

BTW, markup is what you get when you view source in the browser after the PHP is run

hello.. thanks again for the reply..

this is the code for displaying the image’s title, image & feature..
the only problem now is that i dont have a code for displaying the image..
the code only displays the title and feature..


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$pid = $_GET['pid'];
$strSQL = "SELECT * FROM sharp where (pid = '$pid')";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;
if ($num > 0){
$title = mysql_result($rsPIX,$i,"title");
$description = nl2br(mysql_result($rsPIX,$i,"feature"));
}
echo $title . "<br>";
echo $description . "<br>";
?>

A(Answer):


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("upload");
$pid = $_GET['pid'];
$strSQL = "SELECT * FROM sharp where (pid = '$pid')";
$rsPIX = mysql_query($strSQL);
$num=mysql_numrows($rsPIX);
$i=0;
if ($num > 0){
$title = mysql_result($rsPIX,$i,"title");
$description = nl2br(mysql_result($rsPIX,$i,"feature"));
}
echo $title . "<br>";
echo $description . "<br>";
?>

Hi.

You’ve forgotton to put the image in….

Remember the page we made that displayed the imaged using an image tag?… we need to use that again on this page. This time we have the ID though. So it will be something like this….

<img src="pix.php?pixID=<?php echo mysql_result($rsPIX,$i,"PixID"); ?>"/>

Put that in the if statement and it should work!

A(Answer):

Hi.

You’ve forgotton to put the image in….

Remember the page we made that displayed the imaged using an image tag?… we need to use that again on this page. This time we have the ID though. So it will be something like this….

<img src="pix.php?pixID=<?php echo mysql_result($rsPIX,$i,"PixID"); ?>"/>

Put that in the if statement and it should work!

i’ve seen my mistake..
yeah, i’ve tried putting that code..
but i forgot to put this <? ?>
hehe.. thanks again..

this question is out of topic..
are you working?
how old are you friend?

A(Answer):

hey, got a problem again..
a 4th yr.student told me that i should not use ‘longblob’ for saving images because it’s ‘heavy’ for the database and i will have a problem if i export/import the database from one computer to another..

do you have other suggestions for me?
i have only one week to finish this site.. *sighs*

A(Answer):

hey, got a problem again..
a 4th yr.student told me that i should not use ‘longblob’ for saving images because it’s ‘heavy’ for the database and i will have a problem if i export/import the database from one computer to another..

do you have other suggestions for me?
i have only one week to finish this site.. *sighs*

Are you not able to store images on the server?

A(Answer):

I agree with markusn00bs and disagree to an extent….

Agree buecause it is heavy for the server, and you can use image file uploads.

And disagree as if you images are in a DB you can ensure security alot more, like put the image view page behid a log in page

A(Answer):

Are you not able to store images on the server?

the images are only stored in the DB..

A(Answer):

I agree with markusn00bs and disagree to an extent….

Agree buecause it is heavy for the server, and you can use image file uploads.

And disagree as if you images are in a DB you can ensure security alot more, like put the image view page behid a log in page

so, do you have any suggestions?
i am not doing the site now ’cause i might start all over again..

A(Answer):

how many bytes worth of space are all the photos you will be uploading.

my personal preference is to store images in the DB, if the server can’t handle it update the hardware, and software.

A(Answer):

how many bytes worth of space are all the photos you will be uploading.

my personal preference is to store images in the DB, if the server can’t handle it update the hardware, and software.

would there be a problem if i export/import it from one pc to another?

A(Answer):

How much space worth of images are you likely to save in the DB?

is it a commercial project, uni project?

A(Answer):

How much space worth of images are you likely to save in the DB?

is it a commercial project, uni project?

hi.. i’ve already solved this.. hehe.. i’ll be continuing what i am doing.. but i’ll be having a tutorial for server side coding…

A(Answer):

cool. if you’re pnly returning the images one at a time, and you becareful with your "select *" ‘s it won’t slow anything down too much

A(Answer):

OMG!! I’v bin lookin for this for sooooooooooo long! all of u r life savers!!!!! thaaaaaaaaaaaaaaaaaaaaanks!!! I’m a newbie myself ๐Ÿ™ thanks thanks… ๐Ÿ™‚

A(Answer):

Hey everyone…

I’ve tried following this thread, and can’t seem to get the code to work! I’ve been working on this for hours now, and i’m going crazy here lol

When I go to test2.php (which should show me the images), all it shows are image placeholders! It’s pulling the images somewhat correctly, if I upload another image to the table…it’ll show another placeholder, and viewing properties on the images shows the correct URL…the image just isn’t showing up at all ๐Ÿ™ Can anyone help?

Here’s my code:

test.php

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","welcome"))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("emotion");
if (IsSet($_GET['image_id'])){
$gotten = @mysql_query("select image, type from whats_hot where image_id = ".$_GET['image_id']);
list( $type,$data ) = mysql_fetch_row( $query );
header("Content-type: {$type}");
while ($row = mysql_fetch_array($gotten,MYSQL_ASSOC))
{
print $row['image'];
}
mysql_free_result($gotten);
}
?>

test2.php


<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","welcome"))
{
$errmsg = "Cannot connect to database";
}
@mysql_select_db("emotion");
$strSQL = "select * from whats_hot";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<img src="test.php?image_id=<?php echo mysql_result($rsPix,$i,"image_id"); ?>"/>
<?php
$i++;
}
?>

my whats_hot table is set up like so:

image_id (int) auto_increment
image (MEDIUMBLOB)
type (varchar)

A(Answer):

@harshmaul

This page has a lot of great information, and I’m glad Bytes has such active and helpful members. I’m a bit confused though. I was wondering if you could help me out.

I’m trying to achieve basically the same thing that these two pages (pix.php and list.php) seek to create. I’d like to take the images from a database, and show those images to my website users in a nice neat little column. You don’t need to show me any code, you’ve already done that. I just have a few questions.

So the first thing I need to do is set up my tables with a BLOB field labeled imgdata, and a pixID field labeled….. well, what exactly is the pixID field, and how should it be labeled? What are the signifigance of each of these pages? Which part of the code am I just referencing to, and which part actually shows the image? I’d just like a little clarification. I’m awfully new to this. Thanks Bytes.

-Tim

A(Answer):

Hi everyone,

I tried to make this to work but I am having the same problem as vinco83. The images seems to get retrived correctly but it only shows the placholder not image. when I debugged it says that "ID not found in Msyql result index 5"

Looking forward to hear from you soon !

Best regards,
Naim