2015-11-03 30 views
0

我試圖設計一個頁面,在PHP中顯示來自數據庫的圖像。或者我會說只有圖像的位置在數據庫中。php - 無法從數據庫加載圖像

但是,當它顯示圖像..但它確實打印圖像路徑..這意味着它正在獲取圖像路徑沒有任何問題。

這裏是我的代碼:

<?php 

$con = mysqli_connect("localhost", "root", "", "foodies"); 
if(mysqli_connect_errno()){ 
    echo "Failed to connect to mysql"; 
    mysqli_connect_error(); 
} 
    $sql = "select img, name from products"; 
    $result = $con->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     $img = $row["img"]; 
     $name = $row["name"]; 
     //$srcc = "C:\wamp\www\foodies\images"; 
     //$quality=100; 
     //echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; 
     echo "<img src='".$row['img']."' width=200 height=200/>"; 

     ?> 
+0

那麼,路徑錯了?你能舉一個存儲在數據庫中的路徑的例子嗎? –

+1

您的代碼可能包含語法錯誤。 請查閱以下鏈接http://php.net/manual/en/mysqli.error.php和http://php.net/manual/en/function.error-reporting.php 並將其應用於您的代碼。 –

+1

另外,在你評論的'$ srcc =「C:\ wamp \ www \ foodies \ images」; \'應該讀爲'$ srcc =「C:\ wamp \ www \ foodies \ images \ 「;' –

回答

0

對不起,以延遲迴復此問題。這已解決。問題是我從根驅動器開始給定位置,而不是從實際存儲圖像的web文件夾根目錄開始。 例如:網站和圖像位於C:\ WAMP \ my_site \ images \ 而不是從網站根文件夾「\ my_site \ images \」提供位置,我從C:\ WAMP .......

雖然小小的錯誤! ;)

謝謝大家!

0

我不認爲你明白這是怎麼回事與<img>標籤。 src屬性需要是一個URL,它告訴您的Web瀏覽器如何訪問映像,而不是本地文件路徑。 (URL可以用於訪問本地文件,但這似乎並不是你正在做的,而且這也不會幫助你製作網絡就緒軟件。)

+0

我從之前的帖子中看到了同樣的問題。 我也試過了:

0

顯示圖像的最佳方式數據庫是將圖像路徑保存在數據庫表中,然後使用Data URLS Schemes。 試試這個:

<?php $con = mysqli_connect("localhost", "root", "", "foodies"); 
if(mysqli_connect_errno()){ 
echo "Failed to connect to mysql"; 
} 
$sql = "select img, name from products"; 
$result = $con->query($sql); 
if ($result->num_rows > 0) { 
while($row = $result->fetch_assoc()) { 
$img = $row["img"]; 
$name = $row["name"]; 

echo "<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" width=200 height=200/>"; 
?> 

的$ IMG是圖像文件名,不帶擴展名。假設您已經將圖像保存在文件夾「image」中,並且圖像文件擴展名爲jpeg。 我希望這是有幫助的。

0
while($row = mysql_fetch_array($result)) { 
if($row['file_location']=="") 
{ 
//your code 
} 
else 
{ 
?> 
<a href="uploads/<? echo $row['file_location']?>" target="new">VIEW IMAGE</a> 
<?php 
} 
} 
+0

uploads /是文件夾名稱 – rahul