我希望显示从数据库中检索的图像和文本

时间:2022-09-26 15:15:32

I want to show or display image and text both from database using php when I display image form a database then it show image in that page at the same time when I echo some text in that no text is shown in the output page. I mean to say I want to show a employee image as well as it's data in a single page. I'm new in PHP So I do too much R&D on it. but didn't get the result.

我想用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 个解决方案

#1


1  

Try like this,this is working fine in my local system;

试试这个,在我的本地系统中运行得很好;

Code:-

代码:

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

Output:-

输出:

我希望显示从数据库中检索的图像和文本

#2


1  

Currently you changed the doctype of the whole resulting page to an image so no more content could be displayed:

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

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

Assuming that your images are stored in base64 you could use something like the following:

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

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

#3


0  

You must have to define the column name in which you have save text data in your database and fetch it from database and than print it.I have tried to solve your problem in following code.

您必须定义在数据库中保存文本数据的列名,并从数据库中获取它,而不是打印它。我试着用下面的代码来解决你的问题。

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

#1


1  

Try like this,this is working fine in my local system;

试试这个,在我的本地系统中运行得很好;

Code:-

代码:

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

Output:-

输出:

我希望显示从数据库中检索的图像和文本

#2


1  

Currently you changed the doctype of the whole resulting page to an image so no more content could be displayed:

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

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

Assuming that your images are stored in base64 you could use something like the following:

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

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

#3


0  

You must have to define the column name in which you have save text data in your database and fetch it from database and than print it.I have tried to solve your problem in following code.

您必须定义在数据库中保存文本数据的列名,并从数据库中获取它,而不是打印它。我试着用下面的代码来解决你的问题。

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