Displaying image using php mysql

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
Hello again,

I am attempting to display an image using PHP and mysql. The mysql database stores the url to the picture, which is stored on my server. So the url would be something like images/bars/nevermind1.jpg, and I just want the PHP to call up that image and display it in the table.

I think I just have a syntax error, but can't seem to nail it down. I've tried about 1000 different variations (but not the right one!).

Below is the line that does not cause any errors, but of course only displays in text the url to the photo:

echo "<td width='280' height='250' rowspan=3>" . $row['photo']. "</td>";

And now, below is what I think SHOULD display the actual photo, but obviously has some problems since it causes an error:

echo "<td width='280' height='250' rowspan=3>" <img src=\".$row['photo'].\" /> "</td>";

Thanks again for any advice!

Jamie
 

crisp

New Member
Messages
85
Reaction score
0
Points
0
Try this instead

echo "<td width='280' height='250' rowspan=3><img src=\"".$row['photo']."\" /> </td>";
 

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
WORKS! That's absolutely fantastic. You're a lifesaver!

Can anyone enlighten me as to the rules for quotations marks and backslashes in PHP?
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
A string in php is defined between "" or ''.
eg. "text" = 'text'
If you want to use the character " or ' in a string, that's not a problem if you're using the other quotes for your string definition.
eg. "-->'<--" (-->'<--) or '-->"<--' (-->"<--) are not a problem
If you want to use the same quotes in the string as you're using for your string definition, you need to use \
eg. "-->\"<--" (-->"<--) or '-->\'<--' (-->'<--)

But actually it's really easy: don't worry about backslashing any quotes (inside your string), php doesn't care... if you just backslash any sort of quotes you want to use in your string, you'll never have a problem.

"Double and single quotes: \" and '" (Double and single quotes: " and ')
= "Double and single quotes: \" and \'"
 
Last edited:

chibib0

New Member
Messages
46
Reaction score
0
Points
0
Excuse me.. I have a sort of OFFTOPIC question but somehow relevant to this topic..
How to insert an image link to the database?
 

gottaloveit

New Member
Messages
33
Reaction score
0
Points
0
This question I can answer!

chibib0, are you wondering how to insert data into a database or do you already know how to do that?

Assuming you already have set up your database and know how to enter data into it, it's quite easy to add an image link. Just create a new field called "image" or something like that. Then insert the link as text into that field (for instance /images/photo1.jpg). Then make sure you have that image (photo1.jpg in this case) stored in the public_html folder of your server (in this case in the images folder).
 
Top