在php中显示来自数据库的blob图像

时间:2016-07-01 12:57:53

标签: php mysql

我正在尝试显示来自数据库的图像,以便正确的图像显示正确的播放器(它应该显示我在哪里有平均时间的占位符),我一直在阅读较旧的文章,我似乎无法弄明白... 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();

1 个答案:

答案 0 :(得分:3)

您可以使用base64编码的字符串作为图片的来源进行尝试,例如:

echo '<div class="caption"><h3><img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>'. $row['NAME']. '</h3></div>';