Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding and displaying images

Status
Not open for further replies.

dylan42

Programmer
Apr 16, 2008
4
GB


I have created a imagetest table in phpmyadmin and added a image to it, i can view the first image however when i add another image i am having trouble viewing the 2nd image ie 'nokia' can anyone help? my code is as follows only OutBack image is displayed:

<?php
$DB_Host = "xxxxxx";
$DB_Username = "xxxxxx";
$DB_Password = "xxxxx";
$DB_Name = "xxxxxxx";

$ret=mysql_connect($DB_Host, $DB_Username, $DB_Password) or die ("Can no connect");
$db=mysql_select_db($DB_Name);


$sql=" SELECT image FROM `imagetest` WHERE Name = 'OutBack' LIMIT 0 , 30 ";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
$img=$row[0];
header("Content-Type: image/jpeg");
print "$img";

$db=mysql_select_db($DB_Name);
$sql=" SELECT image FROM `imagetest` WHERE Name = 'nokia' LIMIT 0 , 30 ";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
$img=$row[0];
header("Content-Type: image/jpeg");
print "$img";

?>

Thankyou
 
http is not a multipart protocol. you can only send one set of content per request. in the above code you are trying to send two images, and that doesn't work.
 
How do I go about doing it then ??? Please help ...
have you got any examples you may have come across that may help ????
 
you can't. each image needs to be delivered separately. the normal way is to provide an img tag with the src set to a php file with a unique identifier in the query string.
 
can you elaborate on that please - I struggle to understand !!!!

can the image be delivered via qrystring and use database only as this is the important bit - no server or hosting to be done .....
 
It can be delivered via a database. What you can't do is deliver more than 1 of them at the same time in the same script like that

You need to have 2 scripts. ! that shows the images and a second one that picks them out from a database.

To do what you want you need to call your second script form an image tag with the appropriate information.

Code:
<img src="getimg.php?image='OutBack'">
<img src="getimg.php?image='nokia'">
Then in your other script:

Code:
<?php
$DB_Host = "xxxxxx";
$DB_Username = "xxxxxx";
$DB_Password = "xxxxx";
$DB_Name = "xxxxxxx";

$ret=mysql_connect($DB_Host, $DB_Username, $DB_Password) or die ("Can no connect");
$db=mysql_select_db($DB_Name);

if(isset($_GET['image'])){
[green]\\check for url parameter for what image to retrieve.[/green]
[green]\\Make sure parameter is clean so no sql injection can occur.[/green]

$imagename=mysql_real_escape_string($_GET['image']);


}
[green]\\Then you can insert your parameter into your query to deliver your image[/green]
$sql=" SELECT image FROM `imagetest` WHERE Name = '" . $imagename . "' LIMIT 0 , 30 ";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
$img=$row[0];
header("Content-Type: image/jpeg");
print "$img";

PHP online manual entry for mysql_real_escape_string

You should always make sure the parameter you receive from the URL is one you expect and is clean

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
following vacunita's example, i would recommend not including the quotes within the query string of the src attribute of the image tags.

and i really do not recommend storing images (or other binary objects) in a mysql database. there are legion reasons why this is a bad idea. store the images in the filesystem and keep pointers in the database.

if you're going to use a string attribute to act as a unique key for record retrieval, make sure that you url encode the string before including it in the src attribute.
 
Yeah my mistake, the values are better left unquoted.

And I will second jpadie's recommendation about not storing images in a DB. Store the path to the image instead of the image itself. As it is then easier to manage the images.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top