1

I am trying to display an image using php. The image is stored in MYSQL table. I am able to retrieve the information from mysql in my php code, but i am having trouble displaying the image.

   $db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");

   mysql_select_db("gameportal") or die("Could not select database $db_name");

   $query = "select * from gamesinfo;";
   $result = mysql_query($query, $db_link) or die("Could not select");

   $Row = mysql_fetch_row($result);

   echo "$Row[0]<br>";

   echo "$Row[1]<br>";

   echo "<img src="$Row[7]" class="body" alt="" /> <br>";//image

   echo "$Row[5]<br>";

Row 7 contains the location of the image (in this case a weblink). When i try to display the webpage, the page is blank, nothing shows, but when i remove that line with the pic, the webpage shows with the remaining info. What am i doing wrong?

4
  • Can you post an example HTML output of the image. Will give a bit more insight. Commented Mar 4, 2015 at 0:40
  • its a blank white page that shows up, nothing more. Commented Mar 4, 2015 at 0:41
  • Apologies. Misread that. Commented Mar 4, 2015 at 0:46
  • Use mysqli instead of mysql. Object oriented will save you typing. Commented Mar 4, 2015 at 0:54

4 Answers 4

1

This is the culprit:

echo "<img src="$Row[7]" class="body" alt="" /> <br>";

You use unquoted double quotes inside double quotes ;-). Try

echo "<img src='$Row[7]' class='body' alt='' /> <br>";

EDIT

The point is not the double quotes inside double quotes, but unquoted double quotes inside double quotes - this should work as well:

echo "<img src=\"$Row[7]\" class=\"body\" alt=\"\" /> <br>";
Sign up to request clarification or add additional context in comments.

4 Comments

i think it should actually be echo "<img src='".$Row[7]."' class='body' alt='' /> <br>";
@lhoppe this should be the same!
@EugenRieck thanks, that worked, didn't realize i needed to sue single quotes.
@A22asin you just can't mix them up. If you echo with double quotes, use single quotes within, and vice-versa.
0

I missed this the first time but:

<?php

$db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");

mysql_select_db("gameportal")bor die("Could not select database $db_name");

You've got bor instead of or. Make sure to turn PHP errors on.

Comments

0

Try this

Images is a directory where the images are stored.

$dir="images/";

**

echo "<img src='$dir/$row[image]' width=100px height=100px>";

**

It works fine.

Comments

0

Not an exact answer.... but a small piece of advice. I have written an answer because I don't have the reputation for commenting. You can turn on error reporting with this line at the top of php script.

    error_reporting(-1);

Such kinds of errors would be displayed on screen and you would be able to debug yourself. When your work is done you can simply do this...

    error_reporting(0);

Refer this link: PHP error reporting

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.