在Php页面中显示来自数据库的图像

时间:2016-05-01 14:15:44

标签: php database image show

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

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

DB image

我的代码的结果在这里: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']
    )
);

1 个答案:

答案 0 :(得分: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渲染之后)。如果查看代码,可以看到没有任何<img>标记。您呈现的代码是:

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

base64编码的图片是正确的(正如您所见here),但它没有被正确的<img>标记包裹:

<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>";

即使<table><img></table>无效HTML,我认为问题不在您的HTML中,但它在您的javascript中,可能在getQuestion()函数内:对其进行深入测试以检索更正后的代码