GBJpq17
GBJpq17

Reputation: 1

Need help displaying images from mysql to webpage

I have stored images in phpmyadmin in a BLOB format. When I try to display them on my webpage, weird symbols and letters show up. The pictures are a mix of jog and png. Please help.

Here is the code that I currently have.

<?php
session_start();
include 'dbConn.php';

if (!isset($_SESSION['username'])){
  header("Location: clientLogin.php");
}

$query = "SELECT * FROM ip_games ";

$stmt = $dbConn->query($query);
$result = $stmt->fetchAll();


?> 

<style>

body {
background-color: grey;
}

table {

         margin: 0 auto;
    }

td {
         padding-right: 30px;
    }

</style>

<html>
<body>
  <div>
      <form action="welcome.php">
        <input type="submit" value="Sign out" />
      </form>  
 </div>

     <h1>Happy Face Emoji Games</h1>

     <form>
        Name: <?php echo $_SESSION['name'] ?> <br />
      </form>

<table>

         <?
for ($i=0; $i<count($result); $i++)
{

echo "<tr>";
echo "<td>" . $result[$i]["picture"] . "<br />" . $result[$i]["name"]. " <br /> " .$result[$i]["console"] . " <br />" . "$" . $result[$i]["cost"] . "</td>";
//echo "<td>".$result[$i]["console"]."</td>";
//echo "<td>".$result[$i]["description"]."</td>";
//echo "<td>".$result[$i]["cost"]."</td>";
echo "</tr>";
}
?>

</table>

</body>
</html>

Upvotes: 0

Views: 92

Answers (2)

MaxRev17
MaxRev17

Reputation: 393

You need to wrap your image in an <img> tag.

<img src="<?php echo $result[$i]["picture"]; ?>" />

Give that a try and let me know :).

Edit: I wrote that as if it was in the page, for your question you probably want:

echo "<td><img src='" . $result[$i]["picture"] . "' />" . "<br />"... etc.

Upvotes: 1

johnny
johnny

Reputation: 2122

To directly use the binary data as a an image source, you can use the data URI scheme, for example:

$uri = 'data:image/png;base64,'.base64_encode($row['binary-data']);

This URI can then be used directly as the image’s source:

background-image: url(<?php echo $uri; ?>)
<img src="<?php echo $uri; ?>">

not good practice, older browser issues, load issues and catching issues. cheer

Upvotes: 1

Related Questions