我试图在浏览器上显示存储在mysql数据库中的3个图像,但我发现只显示loop WHILE
上第一个迭代的第一个图像。如何在浏览器中显示3个图像?
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
//$id = $_GET['id'];
$sql = "SELECT image,image_type FROM images where id between 2 and 6";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
$result=mysqli_fetch_array($r);
header('Content-Type:image/jpeg');
$ss=$result['image_type'];
while($row = $r->fetch_assoc()){
if ($ss == 'php') {
echo ( $result['image']);
} else if ($ss == 'android') {
echo base64_decode( $result['image'] );
}
}
}else{
mysqli_close($con);
}
?>
此代码:
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
//$id = $_GET['id'];
$sql = "SELECT image,image_type FROM images where id between 1 and 3";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
//$result=mysqli_fetch_array($r);
$result=var_dump(mysqli_fetch_all($r));
//header('Content-Type:image/jpeg');
while($row = mysqli_fetch_assoc($r))
{
var_dump($row) ;
}
/*while($row = $r->fetch_assoc()){
$ss=$row['image_type'];
if ($ss == 'php') {
echo ( $row['image']);
} else if ($ss == 'android') {
echo base64_decode( $row['image'] );
}
}*/
}
else{
mysqli_close($con);
}
?>
,结果如下:
答案 0 :(得分:1)
修改。正如我已经看过这个问题,你应该能够做到:
<img src="data:image/jpeg;base64,
<?php echo base64_encode( $image_data ); ?>
" />
显示所有图片,因此下面应该有效:
while($row = mysqli_fetch_assoc($r)){
$ss = $row['image_type'];
if ($ss == 'php') {
<img src="data:image/jpeg;base64, <?php echo ( $row['image']); ?>" />
} else if ($ss == 'android') {
<img src="data:image/jpeg;base64, <?php echo base64_decode( $row['image'] ); ?> " />
}
}
当您执行 $ r-&gt; fetch_assoc()时(只要您的查询获得结果,您的While循环将继续),您将获取一个结果行数据到关联数组中,因此您可以将数据放在 $ row 变量中。 您必须使用该变量来检查 [&#39; image_type&#39;] 并获取 [&#39;图像&#39;] 。
在您的代码中, $ result [&#39; image_type&#39;] 和 $ result [&#39; image&#39;] 绑定到您使用 $ result = mysqli_fetch_array($ r)获取的第一条记录(作为文档说明:mysqli_result :: fetch_array - mysqli_fetch_array - 将结果行作为关联,数字数组或两者获取)
if($_SERVER['REQUEST_METHOD']=='GET'){
$sql = "SELECT image,image_type FROM images where id BETWEEN 2 and 6";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));
header("Content-type: image/jpeg");
while($row = mysqli_fetch_assoc($r)){
$ss = $row['image_type'];
if ($ss == 'php') {
echo ( $row['image']);
} else if ($ss == 'android') {
echo base64_decode( $row['image'] );
}
}
}else{
mysqli_close($con);
}
当你使用 procedular 样式时,一直使用它,如(来自php.net的例子):
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
或只使用面向对象样式:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
更多信息,示例:mysqli_result::fetch_assoc