0

I am trying to do something I know is probably simple, but I am having the worst time. I have functioning so far: 1.Script to upload image files to server 2. write the image file names to the database 3. I want to retrieve the image filename from the db and add it to the img src tag here is my retrieval script

 <?php

 $hote = 'localhost';
 $base = 'dbasename';
 $user = 'username';
 $pass = '******';
 $cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
 $ret = mysql_select_db ($base) or die (mysql_error ());
 $image_id = mysql_real_escape_string($_GET['ID']);
 $sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
 $result = mysql_query($sql);
 $image = mysql_result($result, 0);

 header('Content-Type: text/html');
 echo '<img src="' $image'"/>';

 ?>

I was trying to pass the Value through image.php?ID=2 but no luck

The PHP script successfully returns the filename, but I cannot for the life of me get it to print it to the html

Any suggestions, please and thank you very much :)

OK, it does return the proper tag, but now it seems as though the script doesnt run to generate the tag. I have tried two ways:

<div class="slides">
    <div class="slide">
        <div class="image-holder">
            <?php
    include ("image.php?ID=2");
    ?>
        </div>

and:

 <img src="image.php?ID=2" alt="" />     

but neither one will insert the filename... I need to identify each img src by the primary key, so I was passing it the ID from each image src location but alas, my PHP ninja skills need to be honed.

Just to clarify: I am uploading images to the server, recording the filenames in a DB and calling that filename in an HTML doc...there are several in each one so I need to pass the ID (i.e. 1,2,3 ) to correspond to the primary key in the table. But I cant get the script to process the tag first. If I go to view source, I can click the script and get the proper result...

Thanks again, you guys and girls are very helpful

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Jjames
  • 183
  • 1
  • 3
  • 14

4 Answers4

5

You're missing the concatenation operator: .:

 echo '<img src="' . $image . '"/>';
maxedison
  • 17,243
  • 14
  • 67
  • 114
2

You can do it as you did but you had the single quotes in twice, (unless you were meaning to use concatenation - which is unnecessary - if you want this see the other answer).

echo "<img src=\"$image\"/>";

Or the longer form with braces if you need to embed inside text.

echo "<img src=\"${image}\"/>";

I'd recommend using heredoc syntax for this if you're doing lots of HTML. This avoids the need to have lots of echo lines.

echo <<< EOF
   <div class="example">
   <img src="$image" />
   </div>
EOF;
Adam
  • 35,919
  • 9
  • 100
  • 137
0

Try using this syntax:

echo "<img src=\"$imagePath\" />";

It works with double quotes provided you escape the quotes in the src attribute. Still not sure why the singles don't work.

mumush
  • 650
  • 6
  • 14
-1

there are 2 major flaws with your design

  1. There is no image.php?ID=2 file on your disk.
  2. There is absolutely no point in including image.php file. You have to get the name right in the file you are working with. don't you have this row already selected from the database? Why not just print the image name then?
  3. And yes, you are using single quotes where double ones needed.
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I am using the image.php but i need to pass it the primary key, and it return the appropriate file name...if you could give me an example of how to give the ID to image.php, and then put the result into the HTML, that would be great... – Jjames Mar 11 '12 at 13:12
  • **There is absolutely no point in including image.php file. You have to get the name right in the file you are working with.** Am I clear enough? – Your Common Sense Mar 11 '12 at 13:19
  • I understand, I dont think there is any reason to be beligerent. – Jjames Mar 11 '12 at 13:43
  • I am just trying to figure out how to pass the ID from the HTML and then return the proper image name from the DB....that's why I am asking more knowledgeable folks than myself. I appreciate you pointing out the flaws, but maybe a little suggestion to go with it might be nice – Jjames Mar 11 '12 at 13:44
  • In essence, I am asking how to reference the script image.php in the HTML properly to get the result, If I am misunderstanding you, please tell me what I am not understanding – Jjames Mar 11 '12 at 13:49