简体   繁体   中英

Displaying an image in PHP from MYSQL?

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?

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>";

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM