在Php / Mysql中显示来自数据库的图像

时间:2016-04-23 11:19:14

标签: php mysql database image

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

以下是我在数据库中的结构:http://imageshack.com/a/img923/8746/Kf16xl.jpg

我的代码是用图片显示问题:

<?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();
}
    if (!empty($image)) {
            $sqlimage  = mysqli_query($connection, "SELECT * FROM questions where 'image' = $image");
            $imageresult = mysqli_query($connection, $sqlimage);
                            while($row=mysqli_fetch_assoc($imageresult))
                                 {
                                    echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
                                 } 
    }
    $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;
       }
    }

&GT;

显示图片的部分是:

  if (!empty($image)) {
         $sqlimage  = mysqli_query($connection, "SELECT * FROM questions where 'image' = $image");
         $imageresult = mysqli_query($connection, $sqlimage);

        while($row=mysqli_fetch_assoc($imageresult))
            {
                 echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
            } 
                         }

非常感谢您分配时间!

0 个答案:

没有答案