2

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>
1
  • i have the following code that should return...sorry! Commented Dec 1, 2012 at 19:39

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I pretty new to web developing. What you says seems obvious now. Thanks a lot to both of you above. Really appreciate the quick reply.
@Ross Don't forget to upvote comments that help you and "accept" the one you find best for you.
2

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>

2 Comments

It was. I see what you are saying. Thanks a lot for the quick response.
No problem! Don't forget to add one of the answers as "accepted" to show you had a proper answer.
-1
<?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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.