简体   繁体   中英

I can' display an image from sql database with php. How is it possible to display it and how can I display more pictures at a time?

I'm trying to display a picture from mysql database, but somehow it isn't working. The picture is in jpeg format. I use a simple php code with a database connect file and a source file. The weird thing is that if I want to download the picture directly from the database, it stays as a binary .bin file. Here are my codes:

db_connect.php:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=probe', $root);
?>

source.php:

<?php
include ‘db_connect.php’;
$query = “select * from users”;
$stmt = $con->prepare( $query );t
$stmt->bindParam($_GET['image']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header(“Content-type: image/jpeg”);
    print $row['image'];
    exit;
}else{
    //if no image found with the given id,
    //load/query your default image here
}
?>

And in the index file I just call the source.php to display the picture:

<img src="../reg/source.php" >

This is the SQL source:

CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `image` blob NOT NULL,
      `image_type` varchar(100) NOT NULL,
      `image_size` varchar(100) NOT NULL,
      `image_name` varchar(100) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `name` (`name`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=44 ;

There are a few issues here and I'm unsure if those curly quotes as I stated in comments are part of your code or not, including the "t" in );t <= delete that "t" if it's part of your code.

If so, ' ' and “ ” need to be changed to ' and " respectively.

However, your connection is using $dbh , and you're using $con in your query.

$dbh = new PDO('mysql:host=localhost;dbname=probe', $root);
^^^^                                                ^^^^^ unknown

and the query:

$stmt = $con->prepare( $query ); // minus the "t" of course.
        ^^^^
  • That would cause your query to fail right there.

However, $root is unknown and not posted.

Check for errors:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Also add $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened.

  • Either $dbh or $con , depending on which variable you are using.

This is how I do upload the picture in the database. You were absolutely right, because I used the code from this site: http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html . But I did some changes in the code.

     <?php
        if(!isset($_FILES['userfile']))
            {
            echo '<p>Please select a file</p>';
            }
        else
            {
            try    {
                upload();

                echo '<p>Thank you for submitting</p>';
                }
            catch(Exception $e)
                {
                echo '<h4>'.$e->getMessage().'</h4>';
                }
            }
        ?>

        <?php 

        function upload(){

        if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
            {
            $size = getimagesize($_FILES['userfile']['tmp_name']);
            $type = $size['mime'];
            $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
            $size = $size[3];
            $name = $_FILES['userfile']['name'];
            $maxsize = 100000;
             if($_FILES['userfile']['size'] < $maxsize )
                {
                $dbh = new PDO("mysql:host=localhost;dbname=probe", 'root', '');
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $stmt = $dbh->prepare("UPDATE users SET image='$imgfp', image_type='$type', image_size='$size', image_name='$name' ");
      $stmt->execute();
                }
            else
                {

                throw new Exception("File Size Error");
                }
            }
        else
            {

            throw new Exception("Unsupported Image Format!");
            }
        }
        ?>

This is how I retrieve the picture.

<div class="row">
            <div class="col-md-3 col-sm-3">

                <form
                    img src="../reg/source.php" method="get" ></form></div></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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