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();
Ketih Carpenter
  • 85
  • 2
  • 3
  • 9

1 Answers1

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>';
eol
  • 23,236
  • 5
  • 46
  • 64
  • While this will work, it has the disadvantage of putting too much load on the initial request and slowing the entire page down. – apokryfos Jul 01 '16 at 13:19
  • 1
    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 – Ketih Carpenter Jul 01 '16 at 13:25
  • @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.. – eol Jul 01 '16 at 13:25
  • 1
    @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 Jul 01 '16 at 13:29
  • 1
    @eol You sir are brilliant, it works fine :) thank you very much – Ketih Carpenter Jul 01 '16 at 13:32