如何使用MYSQLi查询在php中显示数据库中的图像?
下面是我的页面跳转到“prac_图像显示”以显示图像的代码:-如何使用MYSQLi查询在php中显示数据库中的图像?,php,mysql,mysqli,Php,Mysql,Mysqli,下面是我的页面跳转到“prac_图像显示”以显示图像的代码:- <?php session_start(); include("database.php"); $sql = mysqli_query($con,"select * from mst_subject"); echo "<h2><table cellpadding=5>"; while($row = mysqli_fetch_array($sql)){
<?php
session_start();
include("database.php");
$sql = mysqli_query($con,"select * from mst_subject");
echo "<h2><table cellpadding=5>";
while($row = mysqli_fetch_array($sql)){
echo "<tr>";
echo "<td><img height='50' width='50' src='prac_img_display.php? id=".$row['sub_id']."'></td><td><a href=showtest.php?subid={$row['sub_id']}>".$row['sub_name']."</td>";
echo "<tr>";
//$_SESSION['subid']=$row['sub_id'];
}
echo "</table></h2>";
?>
下面是在prac_image_display.php文件中编写的代码:-
<?php
if(isset($_GET['id']))
{
include("database.php");
$sql = "SELECT image FROM mst_subject WHERE
sub_id=".$_GET['id'].";";
//$sql = "SELECT image FROM mst_subject WHERE sub_id=8";
$result=mysqli_query($con,"$sql");
header("Content-type: image/jpeg");
echo mysqli_result($result,0);
mysqli_close($con);
}
else
{
echo 'Please use a real id number';
}
?>
下面是在“database.php”文件中编写的代码:-
图像以BLOB形式存储在数据库中。
问题是,当我在上述两个.php文件中使用MYSQL时,它运行良好,所有图像都能完美地显示在localhost上,但当我用MYSQLi函数替换MYSQL函数时,图像无法显示。请告诉我这里可以做哪些更改。您可以按照以下步骤进行调试,找出实际问题所在 步骤1:更新图像
src
注意:因为有一个空格
第2步:在浏览器中浏览图像URL
,如
注意:检查图像是否正确显示在浏览器窗口中
如果未正确显示图像
步骤3:检查标题(“内容类型:图像/jpeg”)代码>是正确的
假设,如果您上传了PNG
图像,这将不起作用
对于PNG
image,您应该使用标题(“内容类型:image/PNG”)代码>
注意:也不要忘记调试SQL
结果。注意事项。在while循环中,循环中代码的第三行应该是database.php
?在我们看到这一点之前,我们无法判断您遇到了什么问题show your database.php too旁注:您的数据库查询非常容易受到sql注入攻击。请了解结合使用“准备语句”和“参数绑定”的安全好处。您是否检查了mysqli的连接错误?你不能盲目地用mysqli替换所有的mysql函数_
<?php
$con=mysqli_connect("localhost","root","","test") or die("could not
connect ");
//mysql_select_db("test",$con) or die("Could connect to Database");
?>
src='prac_img_display.php? id=".$row['sub_id']."'
/*TO*/
src='prac_img_display.php?id=".$row['sub_id']."'
example.com/prac_img_display.php?id=8