Ross
Ross

Reputation: 49

display image using php

I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".

I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers

<?php


session_start();
$username = $_SESSION['username'];

$con = mysql_connect('localhost','root','password');
mysql_select_db("db");


$profileimage = mysql_query("

SELECT * FROM images WHERE username='$username'

");

$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];

?>

<html>

<img src = "$showimage">


</html>

Upvotes: 2

Views: 72098

Answers (3)

JHM16
JHM16

Reputation: 798

<?php  
    $db = mysqli_connect("localhost:3306","root","","databasename"); 
    $sql = "SELECT * FROM table_name ";
    $sth = $db->query($sql);
    while($result=mysqli_fetch_array($sth)){    
        echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
    }
?>

Works for me

Upvotes: -1

rg88
rg88

Reputation: 20977

First off, HTML doesn't know what "$showimage" means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.

So if the value for $showimage is "/images/foo.jpg" you would need something like:

<img src="<?php echo $showimage; ?>" />

which would give you

<img src="/images/foo.jpg" />

Now, switching things to mysqli is as simple as replacing mysql with mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.

Upvotes: 8

koopajah
koopajah

Reputation: 25632

Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:

<html>
<img src ="<?php echo $showimage; ?>" />
</html>

Upvotes: 2

Related Questions