简体   繁体   English

mysql 中的图像未显示在 php 中

[英]Image not displaying in php from mysql

i am trying to display database value in php, my php code is below我正在尝试在 php 中显示数据库值,我的 php 代码如下

<?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "teia";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";
$result = $conn->query($sql);

if (!empty($result) && $result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $name     = $row['firstname'];
        $lastname = " $row[lastname] ";
        $mobile   = " $row[mobilenumber] ";
        $exp      = " $row[experience] ";
        $photo    = " $row[Photo] ";
    }
} else {
    echo "0 results";
}

this is what i tried to get image这就是我试图获取图像的内容

<?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Photo'] ).'"/>';?>

every data is being displayed except the image, may i know what is the reason for it?除图像外,所有数据都在显示,我可以知道是什么原因吗?

enter image description here enter image description here在此输入图片说明 在输入图片说明

A general practice is to store images in directories on the file system and store references to the images in the database.通常的做法是将图像存储在文件系统的目录中,并将对图像的引用存储在数据库中。 eg path to the image,the image name, etc.. Or alternatively, you may even store images on a content delivery network (CDN) or numerous hosts across some great expanse of physical territory, and store references to access those resources in the database.例如,图像的路径、图像名称等。或者,您甚至可以将图像存储在内容交付网络 (CDN) 或跨越一些广阔物理区域的众多主机上,并存储引用以访问数据库中的这些资源.

As you mentioned that, you have store just title of the image.正如您所提到的,您只存储了图像的标题。 As well as, you have to store the image to particular path.此外,您必须将图像存储到特定路径。

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

You can store the name of the image like:您可以存储图像的名称,如:

$image=basename( $_FILES["imageUpload"]["name"],".jpg"); 

When you want to render then just do like this:当你想渲染时,就这样做:

<img src='uploads/$image_name' height='150px' width='300px'></td>"

Where $image_name will be retrieve from the database从数据库中检索 $image_name 的位置

For Inserting image into database as blob format:将图像以 blob 格式插入数据库:

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//you keep your column name setting for insertion. I keep image type Blob.
$query = "INSERT INTO products (id,image) VALUES('','$image')";  
$qry = mysqli_query($db, $query);

Try to retrieve the image like this:尝试像这样检索图像:

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM