Diaconu Eduard Diaconu Eduard - 7 months ago 94
PHP Question

Display image from database in Php page

I try to make online quiz with images questions, and i need your help/advice. My images is stored on database where have an id "image". My upload works fine, image is stored on database...but i can't show image in questions.

I succeeded to show icon for image and name, but not image. Image is stored in database with base64_encode, and here is a structure from my database.

DB image

And the result from my code is here: http://imageshack.com/a/img922/6874/U4hkbj.jpg

I put name there to verify my connection to database, it's not necessary from final code.

And here is code for display images:

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


And that's my code from show questions with image:

<?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;
}
}
?>


I'm new in php and that's my first project, i really need your help for solve my problem.

Thank you so much for help and for interest!

Answer

You forgot wrapping quotes, comma and you don't echo $row['image']. Change:

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

to:

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

or (coherently with your global syntax) to:

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