2015-05-18 45 views
1

我想要顯示或顯示圖像和文本都從數據庫使用php 當我顯示圖像形式的數據庫,然後它顯示圖像在同一時間在該頁面我回顯了一些文字,輸出頁面中沒有顯示任何文字。 我的意思是說我想在一個頁面上顯示員工圖像及其數據。我是PHP新手所以我做了太多的R & D。但沒有得到結果。我想要顯示圖像以及從數據庫中檢索的文本

<?php 

    $servername = "localhost"; 
    $username = "root"; 
    $dbname  = "dat-database"; 
    $password = ""; 


    $conn = mysqli_connect($servername, $username, $password, $dbname); 

    $emp_id=''; 

    $sql = "select * from emp_personaldetails where EMP_ID='1456'"; 
    $result = mysqli_query($conn,$sql) ; 
    while($row = mysqli_fetch_array($result)){ 
     header('Content-type: image/jpeg'); 
     echo $row['image']; 
     $emp_id=$row['empid']; 
    } 


?> 
<!DOCTYPE html> 
<html> 
    <body> 
     <table> 
      <tr><td>Employee id</td> <td><?php echo $emp_id; ?></td></tr> 
     </table> 
    </body> 
</html> 
+2

如何存儲在數據庫中的圖像? –

+0

'var_dump($ emp_id);' - 它說什麼? – odedta

+0

@rupesh我已經回答你的問題,請檢查並讓我知道。 – RaMeSh

回答

1

像這樣嘗試,這在我的本地系統中工作正常;

代碼: -

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $dbname  = "dat-database"; 
    $password = ""; 

    $conn = mysqli_connect($servername, $username, $password, $dbname); 
    $emp_id=''; 

    $sql = "select * from emp_personaldetails where EMP_ID='1456'"; 
    $result = mysqli_query($conn,$sql) ; 
    while($row = mysqli_fetch_array($result)){ 
     header('Content-type: image/jpeg'); 
     $image=$row['image']; 
     $emp_id=$row['empid']; 
    } 

?> 
<!DOCTYPE html> 
<html> 
    <body> 
     <table> 
      <tr><td>Employee id</td> <td><?php echo $emp_id; ?></td></tr> 
      <tr><td>Employee Image</td> <td><?php echo '<img src="data:image/jpeg;base64,'.base64_encode($image) .'" />';?></td></tr> 
     </table> 
    </body> 
</html> 

輸出: -

enter image description here

1

目前,你改變了整個的結果頁面來所以沒有更多的內容,可以顯示圖像的文檔類型:

header('Content-type: image/jpeg'); 

假設你的圖像存儲用base64你可以使用類似下面的:

$b64Src = "data:img/jpg;base64," . $row["img"]; 
echo '<img src="'.$b64Src.'" alt="" />'; 
0

您必須定義中,你必須保存在數據庫中的文本數據,並從數據庫中獲取它比打印它。我曾經試圖解決您的PROBL列名在下面的代碼中。

<?php 

    $servername = "localhost"; 
    $username = "root"; 
    $dbname  = "dat-database"; 
    $password = ""; 


    $conn = mysqli_connect($servername, $username, $password, $dbname); 

    $emp_id=''; 
    $emp_details=''; 

    $sql = "select * from emp_personaldetails where EMP_ID='1456'"; 
    $result = mysqli_query($conn,$sql) ; 
    while($row = mysqli_fetch_array($result)){ 
     header('Content-type: image/jpeg'); 
     echo $row['image']; 
     $emp_id=$row['empid']; 
     $emp_details=$row['colum_name']; 
    } 


?> 
<!DOCTYPE html> 
<html> 
    <body> 
     <table> 
      <tr><td>Employee id</td> <td><?php echo $emp_id; ?></td></tr> 
      <tr><td>Employee Details</td> <td><?php echo $emp_details; ?></td></tr> 
     </table> 
    </body> 
</html> 
相關問題