[英]Displaying an image in PHP from MYSQL?
I am trying to display an image using php. 我正在尝试使用php显示图像。 The image is stored in MYSQL table. 该图像存储在MYSQL表中。 I am able to retrieve the information from mysql in my php code, but i am having trouble displaying the image. 我能够从php代码中的mysql检索信息,但是我无法显示图像。
$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). 第7行包含图片的位置(在本例中为Web链接)。 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. 您没有bor,而不是or。 Make sure to turn PHP errors on. 确保打开PHP错误。
Try this 尝试这个
Images is a directory where the images are stored. 图像是存储图像的目录。
$dir="images/"; $ DIR = “图像/”;
** **
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. 您可以使用php脚本顶部的这一行打开错误报告功能。
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 请参阅此链接: PHP错误报告
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.