Jetson324
Jetson324

Reputation: 13

Unable to display image from database using PHP

Screenshot of the code: screenshot

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    require ('../../mysqli_oop_connect.php'); //Connect to the database.
    $errors = array(); // Initialize an error array.

// Check for an isbn:
if (empty($_GET['isbn'])) {
    $errors[] = 'You forgot to enter the ISBN-13.';
} else {
    $isbn = $mysqli->real_escape_string(trim($_GET['isbn'])); 
}   

if (empty($errors)) {   
    //Make the query:
    $q = "SELECT * FROM books WHERE isbn=$isbn";
    $r = $mysqli->query($q);
    $num = $r->num_rows;

    if ($num == 0) { // If it fails to run:
        echo "<br /><br /><p>This book is currently not listed in our database. Would you like to add it?</p>\n";
        //HERE IS WHERE IT NEEDS TO REDIRECT TO A PAGE THAT ALLOWS THE CONTENT TO BE ADDED TO THE DB LIKE REGISTER.PHP DOES.
    } else { //If it suceeds then run the query:
        echo '<h1>Is this your book?</h1>';

    while ($row = $r->fetch_object()) { // POST DATA FROM DB.
        echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

The rest of the code is just closing the DB connection.

I don't know what that icon means or where the issue is in the code. The DB is setup with three columns in a table: image, isbn, and name. The goal is to get the book to display the data being retrieved from the database. It is displaying all three items from the database based on the query, except the image is something else. The HTML is recognizing that there is an image being retrieved because when I just do $row->image it freaks out and posts random nonsense, as it should when there is an image being displayed like that. Any help is greatly appreciated.

Upvotes: 0

Views: 2445

Answers (3)

Mujtaba Haider
Mujtaba Haider

Reputation: 1650

echo '<img src="'.$row->image.'" height="100" width="100"><br />' . $row->name . '<br />' . $row->isbn ;

Try this way, and better you print your image path and check if it does exist or does not exist, and yes you can also inspect image element and check path of the image.

Its not clear in the question whether there is image link saved in the db or image data in base64. Answer is given assuming db has image link.

Upvotes: 3

user1864610
user1864610

Reputation:

I'd guess from your comments about 'freaks out and posts random nonsense' that your image is stored in your database, rather than the path to your image. If so, you can't simply echo the image data in your <img> tag. The src attribute requires a URL, which you can create in one of two ways: create a script that serves the image with appropriate headers and use the URL of that script as your src attribute; or, create a data URI and include the data in your page. The latter is probably the simplest, but could increase the load time of your pages. Here's how:

// base64 encode image data and prepend the header
$imgURL = "data:image/png;base64,".base64_encode($row->image);  

echo '<img src="'.$imgURL.'" height="100" width="100">' .
     '<br />' . $row->name . '<br />' . $row->isbn ;

Note - you'll need to change the dataURL header to send the correct image type: image/jpg, image/png, etc.

Upvotes: 1

Giacomo1968
Giacomo1968

Reputation: 26056

You cannot have string substitution with single quotes. It will simply place the text $row->image in the echo:

echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

Try this instead:

echo "<img src='$row->image' height='100' width='100'>" . '<br />' . $row->name . '<br />' . $row->isbn ;

Or this:

echo '<img src="' . $row->image . '" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;

Personally, I prefer using sprintf like this:

echo sprintf('<img src="%s" height="100" width="100">', $row->image) . '<br />' . $row->name . '<br />' . $row->isbn ;

But on top of that, to make your debugging life even easier, I recommend formatting your PHP code to be easier to read like this:

echo sprintf('<img src="%s" height="100" width="100">', $row->image)
   . '<br />'
   . $row->name
   . '<br />'
   . $row->isbn
   ;

The cleaner your code is from the beginning, the easier it is to spot flaws & bugs when they arise.

Upvotes: 0

Related Questions