我想显示图像以及从数据库中检索的文本

时间:2015-05-18 12:08:18

标签: php html sql database

我想使用php在数据库中显示或显示图像和文本 当我从一个数据库显示图像时,它会在我回显一些文本的同时在该页面中显示图像,因为输出页面中没有显示任何文本。 我的意思是说我想在一个页面中显示员工形象及其数据。我是PHP的新手所以我在它上面做了太多的研发。但没有得到结果。

<?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>  

3 个答案:

答案 0 :(得分:1)

目前,您已将整个结果页面的doctype更改为图像,因此无法显示更多内容:

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

假设您的图像存储在base64中,您可以使用以下内容:

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

答案 1 :(得分: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

答案 2 :(得分:0)

您必须定义在数据库中保存文本数据的列名,并从数据库中获取并打印它。我试图通过以下代码解决您的问题。

<?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>