2

i am trying to display an image from a database so the correct image shows up for the correct player (it should show up where i have a placeholder for the mean time) , I have been going through older articles and i cant seem to figure it out... enter image description here

 <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "reapers"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // SELECT (whatever rows you want) FROM (your table name) $sql = "SELECT ID, NAME, image FROM players"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { // Outputting HTML and the data from the DB. Change to $row['the name of the field you want'] echo "<div class='caption'><h3><img src='http://placehold.it/150x150' alt=''><center>" . $row['NAME'] . "</h3></div>"; } } else { echo "No players to show"; } $conn->close(); 
0

1 Answer 1

6

You can try it using a base64 encoded string as the source for the image, e.g.:

echo '<div class="caption"><h3><img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>'. $row['NAME']. '</h3></div>'; 
Sign up to request clarification or add additional context in comments.

5 Comments

While this will work, it has the disadvantage of putting too much load on the initial request and slowing the entire page down.
thanks for this, it does take the picture in now :) where do i add the $row['NAME'] now so the name is also still shows up like it did before
@apokryfos That's true and definitely needs to be considered. For a few player icons it might be sufficent though. Personally, I tend to avoid storing images in the db, rather just the link to the images..
@KetihCarpenter I've updated the code to show the name as well. Hope I didn't miss a symbol/tag, I'm on my mobile right now.
@eol You sir are brilliant, it works fine :) thank you very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.