I have all my images store in a folder with a file path referenced in a database table along with additional image tag attributes such as alternative text. I have managed to output the image to the browser without an issue but when i try to add the alt text from the database there is no closing tag for the image tag.

I have noticed from looking at the outputted html that it there is no space between the src and alt attributes which is i assume is the reason why it cant find the closing part of the image tag.

This is the code i have so far, does anyone know how to put a space between the src and alt attributes or a better way to output the image altogether. I am grateful of any help

	$image =@mysql_query("SELECT * FROM tblphoto WHERE photoid='1'");
		if (!$image) {
			exit('<p>Error Performing query: ' . mysql_error() . '</p>');

		while ($row = mysql_fetch_array($image)) {
		echo "<img src=".$row['photosrc']. "alt=" .$row['altText']."/>";

and this has the following effect in HTML

<img src="../images/meeting.jpg"alt="Meeting of CDM coordinators"/>

actually your output is <img src=../images/meeting.jpgalt=Meeting of CDM coordinators/> html requires quotes
php requires quotes
together requires 2 sets quotes (&escapes)
put space in the code where you want space to be, space inside literal text is output, echo "<img src=\"".$row['photosrc']."\" alt=\"".$row['altText']."\"/>"; outputs <img src="myjpg.jpg" alt="myJpeg" />

php will parse $variables inside dquotes, but is slower.
If you use dquotes when you arent making use of $variables it is slower as php will search each literal string for $variables echo '<img src=\"'.$row['photosrc'].'\" alt=\"'.$row['altText'].'\" />'; image dimensions should(can) be got from the image on upload(serving) and stored in the database(output directly) for w3c compliant html (UR using self closing image tags />) getimagesize() on upload is probably best as its only done once

echo '<img src=\"'.$row['photosrc'].'\" alt=\"'.$row['altText'].'\" width=\"'.$row['photowidth'].'\" height=\"'.$row['photoheight'].'\" />';


Thanks for reply to my post however it doesnt seem to work. The uses of quotes and slashes seem to stop the image from showing altogether and now part of the alt text is in its place. It appears on the web page as


missing the rest of the alt tag and no image visible

Looking at the HTML source code shows this

<img src=\""../images/meeting.jpg"\" alt=\""Meeting of CDM coordinators"\" />

which is not a valid img tag. Thanks for you help

If you gets quotes from your original script, there quotes stored in your database
if the filename was stored as "filename" you would get this problem from the code scrap I posted
If there is,
(apart from a database design that may or may not cause problems when you try to manipulate the data later)
then all you have to do
is put in the space echo "<img src=".$row['photosrc']." alt=".$row['altText']."/>"; and add in the width height
head slap moment
escape single quotes inside single quotes
escape dquotes inside dquotes

echo '<img src="'.$row['photosrc'].'" alt="'.$row['altText'].'" width="'.$row['photowidth'].'" height="'.$row['photoheight'].'" />';

If anything can go wrong it will

Thanks for your help.

Hi All,

The above example works which i have applied it to show an image in a column. If i wanted to show further images for the product, say by clicking on a hyperlink which opens a popup window to show further images, how can that be done?

Thanks, any help will be appreciated

studio if your problem is resolved you should mark your thread as solved, its the better way to say thanks the person who helped you.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.