【问题标题】:How to get images from database (blob column) on the same page using pdo or mysqli ?? PHP如何使用 pdo 或 mysqli 从同一页面上的数据库(blob 列)获取图像? PHP
【发布时间】:2016-11-16 04:08:38
【问题描述】:

我在从数据库中获取图像时遇到问题。我也浏览了很多堆栈和其他网站的文章,但没有一个有我正在寻找的解决方案。如何在页面上获取存储在数据库中的图像?

这是代码,请检查并告诉我我犯了什么错误。非常感谢所有帮助。

<?php
    // I have database name databaseimage
    // I have a table named store
    // I have 3 columns into the table store
    // id (int primary key ai), name (varchar), and image (longblob)

// prevent accesing the page directly
if($_SERVER['REQUEST_METHOD'] !='POST') {
    echo "you can not acces this page directly";
    die();
} 
else {

print_r($_FILES);


// file properties

$file = $_FILES['image']['tmp_name'];
echo "<br>$file";

if(!isset($file)){
    echo "please select an image";
    die();
} else {
//actual image
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])) ;

 // image name
    $image_name = addslashes($_FILES['image']['name']);
    echo "<br> image name is = $image_name";

//geting image size to check whether it is actually an image or not
$image_size = getimagesize($_FILES['image']['tmp_name']); 
echo "<br>";
print_r($image_size);

    if($image_size==FALSE) {
        echo "plese select an images only";
            die();
        }
    else {

        #code to put an image into the database

                // connect to database

try {
    $con = new PDO("mysql:host=localhost;dbname=databaseimage", "root",
     "");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "<br>connection succesfull";

    // make a query
    $extr = $con->prepare("INSERT INTO store (name, image) 
                                    VALUES (:na , :im)" );

    //$a=1;
    //$extr->bindParam(':i', $a);
    $extr->bindParam(':na', $image_name);
    $extr->bindParam(':im', $image, PDO::PARAM_LOB);

    if ( $extr->execute()==true) {
        echo "<br>image uploaded succesfully";

 # insert is working. I can insert images into database

 # but really facing problem while displaying those images
 # below is the code I tried

 # CODE for to show uploaded files
 #  to show images which is uploaded
        $show = $con->prepare("SELECT * FROM store ");
        //$a = 1;
        //$show->bindParam(':iam', $a);
        $show->execute();



        while ($row = $show->fetch(PDO::FETCH_BOUND) ) {
            # SHOW IMAGES 

             //echo "<img src = '$row[image]' alt='image'  >";
echo '<img src= "data:image/png;base64,'.base64_encode($row['image']).'" 
                                    height="100" width="100" />';
        }


} else {
    echo "<br>image not uploaded";
    }

}
catch(Exception $e)
    {
    echo "<br> Connection failed: " . $e->getMessage();
    }
        }
}

// disconnect the connection
$con = null;
}
?>

【问题讨论】:

    标签: php mysqli pdo


    【解决方案1】:

    第 1 步:创建一个文件名为 getimage.php 的 php 脚本 以下代码:

    <?php
    $id = $_GET['id'];
    // do some validation here to ensure id is safe
    
    $con = new PDO("mysql:host=localhost;dbname=databaseimage", "root",
     "");  
    $sql = "SELECT image FROM store WHERE id=$id";
    $stmt=$con->query($sql);
    $res=$stmt->fetch(PDO::FETCH_ASSOC);
    $con=null;    
    
    header("Content-type: image/pn")g;
    echo $res['image'];
    ?>
    

    第 2 步:在您当前的 php 页面中(在注释“# 下面是我尝试过的代码”下方)尝试以下代码:

    $show = $con->prepare("SELECT id FROM store ");
    //$a = 1;
    //$show->bindParam(':iam', $a);
    $show->execute();
    
    while ($row = $show->fetch(PDO::FETCH_NUM) ) {
        # SHOW IMAGES              
        echo '<img src="getimage.php?id="'.$row[0].'" 
                                    height="100" width="100" />';
    }
    

    【讨论】:

    • 谢谢 Ashish.. 我试过了,但它显示错误 Parse error: syntax error, unexpected '=' in (file path here)。 @ashishKumar
    • 我已经更正了解析错误。请参阅 img HTML 元素中的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2019-04-19
    • 2014-04-15
    • 2010-10-23
    相关资源
    最近更新 更多