如何从数据库 MYSQLi 中显示 php 中的图像?
How to display images in php from database MYSQLi?
我想使用 Mysqli 从我的数据库中显示 php 中的图像。
这是我在 question.php:
中的代码
<form class="form-horizontal" role="form" id='login' method="post" action="result.php">
<?php
$row = mysqli_query( $conn, "select id,question_name,image from questions where category_id=1 and level_id=1 ORDER BY RAND() LIMIT 10");
$i = 0;
$j = 1; $k = 1;
?>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
//if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?></p>
<?php echo "<img src='".$result['image']."'/>";?>
<input type="text" name="reponse" required />
<br/>
</div>
<?php
$k++;
} ?>
</form>
我的 table 是:
但结果是这样的:
你能帮我正确显示我的图片吗?
您可以通过 base64_encode()
ing the binary data to the browser using the data-uri scheme:
<?php echo '<img src="data:image/png;base64,'.base64_encode($result['image']).'" />'; ?>
或者,您可以创建一个 image.php 脚本,它接受一个 ID,并将二进制数据直接输出到浏览器:
$query = mysqli_query("SELECT * FROM ..."); //**Bind** $_GET['ID'] to the query:
$row = mysqli_fetch_assoc($query);
header('Content-type: image/png');
echo $row['image'];
在您之前的代码中,访问方式如下:
<?php echo '<img src="image.php?id=', $row['id'], '" />'; ?>
我想使用 Mysqli 从我的数据库中显示 php 中的图像。 这是我在 question.php:
中的代码 <form class="form-horizontal" role="form" id='login' method="post" action="result.php">
<?php
$row = mysqli_query( $conn, "select id,question_name,image from questions where category_id=1 and level_id=1 ORDER BY RAND() LIMIT 10");
$i = 0;
$j = 1; $k = 1;
?>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
//if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?></p>
<?php echo "<img src='".$result['image']."'/>";?>
<input type="text" name="reponse" required />
<br/>
</div>
<?php
$k++;
} ?>
</form>
我的 table 是:
但结果是这样的:
你能帮我正确显示我的图片吗?
您可以通过 base64_encode()
ing the binary data to the browser using the data-uri scheme:
<?php echo '<img src="data:image/png;base64,'.base64_encode($result['image']).'" />'; ?>
或者,您可以创建一个 image.php 脚本,它接受一个 ID,并将二进制数据直接输出到浏览器:
$query = mysqli_query("SELECT * FROM ..."); //**Bind** $_GET['ID'] to the query:
$row = mysqli_fetch_assoc($query);
header('Content-type: image/png');
echo $row['image'];
在您之前的代码中,访问方式如下:
<?php echo '<img src="image.php?id=', $row['id'], '" />'; ?>