从MySql获取BLOB图像到CSS内联样式

时间:2018-09-05 05:20:50

标签: php css mysql

我有一个php脚本,其中包含div html标记。并且我想将MySql中的css background-image设置为每个div。

我的错误代码:

<?php
require 'config.php';
$query = "SELECT * FROM `users`";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
    $user_Image = $row['blob'];
    echo "<div class='avatar' style='background-image :url(\"data:image/jpeg;base64, base64_encode($user_Image)\");'></div>";
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:2)

尝试

      <?php
require 'config.php';
$query = "SELECT * FROM `users`";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
    $user_Image = $row['blob'];
    ?>
    <div class="avatar" style="background-image :url(data:image/jpeg;base64, base64_encode(<?php echo $user_Image;?>)")></div>
<?php     
}
$conn->close();
?>