【问题标题】:Display image from database in Php page在 PHP 页面中显示数据库中的图像
【发布时间】:2016-08-26 08:15:26
【问题描述】:

我尝试使用图像问题进行在线测验,我需要您的帮助/建议。我的图像存储在具有 ID“图像”的数据库中。我的上传工作正常,图像存储在数据库中...但我无法在问题中显示图像。

我成功显示了图像和名称的图标,但没有显示图像。图像使用base64_encode存储在数据库中,这是我数据库中的一个结构。

我的代码的结果在这里:http://imageshack.com/a/img922/6874/U4hkbj.jpg

我把名字放在那里是为了验证我与数据库的连接,最终代码中没有必要。

这是显示图像的代码:

 require_once("scripts/connect_db.php");
    $res=mysqli_query($connection, "SELECT * FROM questions WHERE id='$question'");

            echo "<table>";
            while($row=mysqli_fetch_array($res)) {

                echo "<tr>";
                echo "<td>";?> <img src= data:image/png;base64 ' . $row['image']; .  '  > <?php echo "</td>";
                echo "<td>" ; echo $row["name"]; echo" </td>";

                echo "</tr>";

            }
            echo "</table>";

这是我的显示问题的代码:

   <?php 
   session_start();
   require_once("scripts/connect_db.php");
   $arrCount = "";
    if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysqli_query($connection, "SELECT id FROM questions");
$numQuestions = mysqli_num_rows($sql);
if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
    $currQuestion = "1";
}else{
    $arrCount = count($_SESSION['answer_array']);
}
if($arrCount > $numQuestions){
    unset($_SESSION['answer_array']);
    header("location: index.php");
    exit();
}
if($arrCount >= $numQuestions){
    echo 'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
            <form action="userAnswers.php" method="post">
            <input type="hidden" name="complete" value="true">
            <input type="text" name="username">
            <input type="submit" value="Finish">
            </form>';
    exit();
}


    require_once("scripts/connect_db.php");
    $res=mysqli_query($connection, "SELECT * FROM questions WHERE id='$question'");

            echo "<table>";
            while($row=mysqli_fetch_array($res)) {

                echo "<tr>";
                echo "<td>";?> <img src= data:image/png;base64 ' . $row['image']; .  '  > <?php echo "</td>";
                echo "<td>" ; echo $row["name"]; echo" </td>";

                echo "</tr>";

            }
            echo "</table>";




    $singleSQL = mysqli_query($connection, "SELECT * FROM questions WHERE id='$question' LIMIT 1");
                    while($row = mysqli_fetch_array($singleSQL)){
        $id = $row['id'];
        $thisQuestion = $row['question'];
        $type = $row['type'];
        $question_id = $row['question_id'];
        $q = '<h2>'.$thisQuestion.'</h2>';
        $sql2 = mysqli_query($connection, "SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
        while($row2 = mysqli_fetch_array($sql2)){
            $answer = $row2['answer'];
            $correct = $row2['correct'];
            $answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label> 
            <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
            ';

        }
        $output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
        echo $output;
       }
    }
?>

我是 php 新手,这是我的第一个项目,我真的需要你的帮助来解决我的问题。

非常感谢您的帮助和关注!

编辑:图像使用以下代码存储在数据库中:

if (isset($_FILES['image'])) {
$name = $_FILES['image']['tmp_name'];
$image = base64_encode(
    file_get_contents(
        $_FILES['image']['tmp_name']
    )
);

【问题讨论】:

  • 不是base64_encode($row['image'])
  • @FastSnail,首先,感谢您的评论。我替换为 ,不幸的是,结果相同。 :(

标签: php database image show


【解决方案1】:

您忘记了引号和逗号,并且您没有回显$row['image']。变化:

<img src= data:image/png;base64 ' . $row['image']; .  '  >

到:

<img src="data:image/png;base64,<?= $row['image'] ?>"  >

或(与您的全局语法一致)到:

echo '<img src="data:image/png;base64,' . $row['image'] . '">';

编辑:

我在您最后的评论中看到了 pastebin。首先,我认为它不是 HTML 源代码,而是来自页面检查器的源代码(在 DOM 渲染之后)。如果查看代码,您可以看到没有任何&lt;img&gt; 标签。您呈现的代码是:

<div id="answers">/9j/(...)/2Q=="&gt;<h2>
                  └────────────┘
                   base64 image

base64 编码的图像是正确的(如您所见here),但它没有被正确的&lt;img&gt; 标签包裹:

<img src="data:image/png;base64,/9j/(...)/2Q==">
                                └────────────┘
                                 base64 image

查看您的代码:

echo "<table>";
while($row=mysqli_fetch_array($res)) {
    echo '<img src="data:image/png;base64,' . $row['image'] . '">';
}
echo "</table>";

即使&lt;table&gt;&lt;img&gt;&lt;/table&gt; 不是有效的 HTML,我认为问题不在于您的 HTML,而在于您的 javascript,可能在 getQuestion() 函数内部:对其进行深入测试以检索更正的代码。

【讨论】:

  • 非常感谢您的帮助。几天前,我尝试了类似的方法,但我不明白我的结果是什么字符串:imageshack.com/a/img923/2690/2AZKay.jpg 也许,是我的上传格式有问题,对吧?再次感谢您的帮助! :)
  • 我的代码,显示一个字符串(可能是base64)。我想现在我需要一个函数来解码 base64。带有结果的打印屏幕在此处上传:imagizer.imageshack.us/a/img923/2690/2AZKay.jpg。奇怪的是为什么不显示提交按钮:))。感谢您花时间回答我的问题! :)
  • 如果你想输出一个base64数据img,你不必解码它。首先,检查这个url:如果你看到一个红点,你的浏览器可以读取data:image;所以,问题可能出在您的原始编码中。你如何编码原始图像?
  • 我不知道如何感谢您的帮助...我用上传代码编辑了我的原始帖子(它更容易阅读)。那里可能有问题吗?
  • 我无法重现您的问题:它对我来说很好。您可以将您的 html 源代码(不要使用检查器,使用“查看源代码”)粘贴到 pastebin 中吗?
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2017-01-30
相关资源
最近更新 更多