2016-07-25 189 views
-1

我已將圖像插入數據庫並在表中存儲名稱。 我的圖像保存在名爲「上傳」的文件夾中。 現在需要從數據庫檢索圖像並顯示它。當我嘗試顯示時,它只顯示從我的桌子上取下的圖像名稱,但它不顯示圖像。下面如何從數據庫檢索圖像並在網頁上顯示圖像

$sql="SELECT * FROM candi_profile WHERE can_email='{$_SESSION['usr_email']}'"; 

$result=mysqli_query($con,$sql); 
if(!$result) die(mysqli_error($con)); 
    <div class="container"> 

     <!-- Page Header --> 
     <div class="row"> 
      <div class="col-lg-12"> 
       <h1 class="page-header">Employer Dashboard 

       </h1> 
      </div> 
     </div> 
     <!-- /.row --> 

     <!-- Projects Row --> 

     <div class="row"> 
      <div class="col-md-4"> 
     <?php 
      while($rows=mysqli_fetch_array($result)){ 
       $c_id = $rows['can_id']; 
      var_dump($c_id); 
      ?> 

      <p class="lead"><?php echo $rows['can_name'] ?></p> 
      <div class="profile-sidebar"> 
       <!-- SIDEBAR USERPIC --> 

       <div class="profile-userpic"> 
         <p class="lead"> 
         <?php echo $rows['pic_name'] ?></p> 
       </div> 

       <!-- END SIDEBAR USERPIC --> 

       <!-- SIDEBAR USER TITLE --> 
       <div class="profile-usertitle"> 
        <div class="profile-usertitle-name"> 
         Marcus Doe 
        </div> 
        <div class="profile-usertitle-job"> 
         <?php echo $rows['can_city'] ?> 
        <i class="glyphicon glyphicon-map-marker"> 
         </i> 
        </div> 

        <div class="profile-usertitle-job"> 
         <i class="glyphicon glyphicon-envelope"></i> 
         <?php echo $rows['can_email'] ?> 
        </div> 

        <div class="profile-usertitle-job"> 
         <?php echo $rows['can_country'] ?> 
        </div> 
       </div> 
       <!-- END SIDEBAR USER TITLE --> 

       <!-- SIDEBAR BUTTONS --> 
       <div class="profile-userbuttons"> 
        <hr> 
       </div> 

       <!-- END SIDEBAR BUTTONS --> 
       <!-- SIDEBAR MENU --> 

      <?php 
      } 
      ?>   
      </div> 

enter image description here enter image description here

+0

添加您的您存儲image.Like'HTTP完整路徑://本地主機/測試/ images/test1.php' –

+0

你必須只存儲數據庫中的圖像而不是圖像。 –

+0

這種問題已經被問過千次之前 – e4c5

回答

-1

您可以使用此代碼從數據庫中檢索圖像

<?php 
include 'connection.php' 
?> 
<?php 

$result = mysql_query("SELECT * FROM table") or die(mysql_error()); 


?> 

<table border="1" cellpadding="5" cellspacing="5"> 
<tr> <th>Image</th></tr> 

<?php 

while($row = mysql_fetch_array($result)) { 

$id = $row['id']; 

?> 
    <tr> 

     <td><img src="uploads/<?php echo $row['pic_name'];?>" alt=" " height="75" width="75"></td> 

    </tr> 

<?php 
} 
} 

?> 
</table> 
+0

你不覺得上面給出的評論狀態相同嗎? BTY不必要的代碼 –

+0

感謝它現在的工作:) – Deepashika

+0

使用表格進行格式化顯示,這是非常糟糕的代碼,應該使用CSS並正確使用div標籤和格式。 – user3750649

1

使用圖像標籤

檢索代碼提供給顯示圖像,並給它的圖像的路徑文件夾

<img src="your path/<?php echo $rows['pic_name'] ?>" /> 
+0

我認爲在你的評論它是返回圖像的路徑不顯示它。 –

+0

(<?php echo'/images/'.$rows['pic_name']?>)這是您的評論。 –

+0

我不認爲它會顯示圖像。 –

1

我認爲內容$ rows ['pic_name']只是字符串,就像你說的那樣q題目了。

放入一個圖像屬性,並調用圖像的路徑與相應的文件名保存在數據庫中。

<img src = "<path>/<?php echo $rows['pic_name'] ?>" /> 

注意

確保圖像現有的你的慾望之路。

+0

感謝它現在的工作:) – Deepashika

0

的朋友,而不是把圖像文件夾,你應該做一個新的圖像列(即「imageColumn」),那麼 您需要創建其他類型的斑點PHP腳本來返回圖像數據,例如getImage.php。

home.php(或顯示圖像頁)代碼

<body> 
<img src="getImage.php?id=1" width="175" height="200" /> 
</body> 

然後getImage.php是

<?php 

    $id = $_GET['id']; 
    // do some validation here to ensure id is safe 

    $link = mysql_connect("localhost", "root", ""); 
    mysql_select_db("dvddb"); 
    $sql = "SELECT imageColumn FROM Tablename WHERE id=$id"; 
    $result = mysql_query("$sql"); 
    $row = mysql_fetch_assoc($result); 
    mysql_close($link); 

    header("Content-type: image/jpeg"); 
    echo $row['imageColumn ']; 
?> 
相關問題