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?

Adam.Biczo

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 ;
Funk Forty Niner

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I display an image stored in a database on a php page?

From Dev

How can I display data from a SQL database to a textbox

From Dev

How can I display the Epoch time in JavaScript?

From Dev

How can i display correctly time

From Dev

How can I display one object at a time?

From Dev

How can I display a Media picker image

From Dev

How can I display the image name on mouseover?

From Dev

How can I display an image using Pillow?

From Dev

How can I display a paragraph over image?

From Dev

How can I display an image in a JPanel

From Dev

how can I Display image in ExoPlayer?

From Dev

How can I display image of blob type in php?

From Dev

.php how to display an image that I can change and stock on my directory

From Dev

how can I display all function name from cscope database?

From Dev

how can I display saved values from the database to wysiwyg tinymce

From Dev

How can I display image file object from java to jsp?

From Dev

How to I display image from mysql PHP

From Dev

How can i store image and display it in webpage from database in laravel 4

From Dev

How can I display an image from my database to a view page in CodeIgniter?

From Dev

How can I display Loops

From Dev

How can i display info?

From Dev

How Can I get a full result from a sql query using php and then display that with javascript

From Dev

How can I display my page action on more sites?

From Dev

How can I display my page action on more sites?

From Dev

How can I display two screenful of a file using the 'more' utility?

From Dev

How can I display an image on top of an image in PyGame?

From Dev

How can I display binary image data as an Image in PHRETS

From Dev

How can I display an image on top of an image in PyGame?

From Dev

How can I display world time in javascript using php time() as a base time?

Related Related

  1. 1

    How can I display an image stored in a database on a php page?

  2. 2

    How can I display data from a SQL database to a textbox

  3. 3

    How can I display the Epoch time in JavaScript?

  4. 4

    How can i display correctly time

  5. 5

    How can I display one object at a time?

  6. 6

    How can I display a Media picker image

  7. 7

    How can I display the image name on mouseover?

  8. 8

    How can I display an image using Pillow?

  9. 9

    How can I display a paragraph over image?

  10. 10

    How can I display an image in a JPanel

  11. 11

    how can I Display image in ExoPlayer?

  12. 12

    How can I display image of blob type in php?

  13. 13

    .php how to display an image that I can change and stock on my directory

  14. 14

    how can I display all function name from cscope database?

  15. 15

    how can I display saved values from the database to wysiwyg tinymce

  16. 16

    How can I display image file object from java to jsp?

  17. 17

    How to I display image from mysql PHP

  18. 18

    How can i store image and display it in webpage from database in laravel 4

  19. 19

    How can I display an image from my database to a view page in CodeIgniter?

  20. 20

    How can I display Loops

  21. 21

    How can i display info?

  22. 22

    How Can I get a full result from a sql query using php and then display that with javascript

  23. 23

    How can I display my page action on more sites?

  24. 24

    How can I display my page action on more sites?

  25. 25

    How can I display two screenful of a file using the 'more' utility?

  26. 26

    How can I display an image on top of an image in PyGame?

  27. 27

    How can I display binary image data as an Image in PHRETS

  28. 28

    How can I display an image on top of an image in PyGame?

  29. 29

    How can I display world time in javascript using php time() as a base time?

HotTag

Archive