无法显示图像路径存储在数据库中的图像

时间:2014-10-15 10:44:27

标签: php html mysql sql database

我正在尝试在网页上显示图像,其中存储在数据库和图像中的图像路径存储在服务器中。但是我无法使用以下代码显示这些图像,所以请有人帮我解决这个问题,..

enter image description here

<form method="post"  enctype="multipart/form-data" action="file_upload.php">
<table>

<?php

$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxxx';
$dbpass = 'xxxxxxxxxx';
$db_name = 'xxxxxxxxxx';
$tbl_name = 'xxxxxxxxxxx';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("$db_name")or die("cannot select DB");

$query1 = mysql_query("select * from '$tbl_name' where id='1'");
$rows1 = mysql_fetch_array($query1);
$path1 = $rows1['image'];

$query2 = mysql_query("select * from '$tbl_name' where id='2'");
$rows2 = mysql_fetch_array($query2);
$path2 = $rows2['image'];

$query3 = mysql_query("select * from '$tbl_name' where id='3'");
$rows3 = mysql_fetch_array($query3);
$path3 = $rows3['image'];

echo '<tr><td><img src="$path1"></td>' ;
echo '<td><img src="$path2"></td>' ;
echo '<td><img src="$path3"></td></tr>' ;

?>

</form>
</table>

7 个答案:

答案 0 :(得分:2)

看看这段代码:

$b = "aaaaaaa";

echo '"$b"';  // will displayt "$b"
echo "'$b'";  // will displayt 'aaaaaaa'
echo "\"$b\"";// will displayt "aaaaaaa"

enter code here

所以,对于你的问题,这只是因为如果你想要一个变量用字符串计算,你必须把它放在“”而不是“”之间。

<table>
<?php 
	$path1 = "/upload/image1.png";
	$path2 = "/upload/image2.png";
	$path3 = "/upload/image3.png";
	
	echo "<tr><td><img src='$path1'></td>" ;
	echo "<td><img src='$path2'></td>" ;
	echo "<td><img src='$path3'></td></tr>" ;
?>
</table>

或者

<table>
<?php 
	$path1 = "/upload/image1.png";
	$path2 = "/upload/image2.png";
	$path3 = "/upload/image3.png";
	
	echo '<tr><td><img src="'.$path1.'"></td>' ;
	echo '<td><img src="'.$path2.'"></td>' ;
	echo '<td><img src="'.$path3.'"></td></tr>' ;
?>
</table>

答案 1 :(得分:0)

如果当前路径中有uploads目录,请尝试以下操作 - 注意$ pathX前面的period告诉,它是当前目录。

echo '<tr><td><img src=".$path1"></td>' ;
echo '<td><img src=".$path2"></td>' ;
echo '<td><img src=".$path3"></td></tr>' ;

答案 2 :(得分:0)

(停止使用 mysql _ * 函数,因为它们长期折旧)

使用 mysqli PDO

在您的示例中使用循环,以便轻松访问结果集中的所有行,如下所示

$query1 = mysql_query("select * from ".$tbl_name);
while($rows = mysql_fetch_array($query1))
{
$path = $_SERVER['DOCUMENT_ROOT'].$rows['image'];
?>
<tr><td><img src="<?=$path?>"></td>
<?php
}

答案 3 :(得分:0)

变化:

mysql_query("select * from '$tbl_name' where id='3'");

mysql_query("select * from '".$tbl_name."' where id='3'");

答案 4 :(得分:0)

<table>

<?php

try
{
$db = new PDO('mysql:host=localhost;dbname=test', 'root', 'password');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}

$query = $db->query("SELECT * FROM '$tbl_name'")->fetch();

echo '<tr>';

foreach($query as $k => $v){
    echo '<td><img src="' . $v['image'] . '"></td>' ;
}

echo '</tr>';
?>

</table>

答案 5 :(得分:0)

**Try it**

<table>

    <?php

    $dbhost = 'xxxxxxxx';
    $dbuser = 'xxxxxxxxx';
    $dbpass = 'xxxxxxxxxx';
    $db_name = 'xxxxxxxxxx';
    $tbl_name = 'xxxxxxxxxxx';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    if(!$conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("$db_name")or die("cannot select DB");

    $query = mysql_query("SELECT * FROM $tbl_name");
    while($array = mysql_fetch_assoc($query))
    {
    echo '<tr>';

        echo "<td><img src='". $v['image'] ."' width='200' height='200'></td>" ;
    echo '</tr>';
    }
    ?>

    </form>
    </table>

答案 6 :(得分:-1)

您的查询存在问题:

mysql_query("select * from '$tbl_name' where id='3'");

如果你这样写,变量$ tbl_name就不会得到解决。

试试这样:

mysql_query("select * from '".$tbl_name."' where id='3'");

图像元素的src属性相同