我是php的新手,正在尝试从我的MySQL数据库中检索图像。
运行以下脚本后,我收到“请检查ID!”信息。我错过了什么吗?
CREATE TABLE tbl_images(
id tinyint( 3 ) unsigned NOT NULL AUTO_INCREMENT ,
image blob NOT NULL ,
PRIMARY KEY ( id )
)
<?php
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
# Connect to DB
$link = mysqli_connect("$host", "$username", "$password") or die("Could not connect: " . mysqli_error());
mysqli_select_db("$database") or die(mysqli_error());
# SQL Statement
$sql = "SELECT `image` FROM `tbl_images` WHERE id=" . mysqli_real_escape_string($_GET['id']) . ";";
$result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());
# Set header
header("Content-type: image/png");
echo mysqli_result($result, 0);
} else
echo 'Please check the ID!';
?>
答案 0 :(得分:3)
因此,您要么不提供身份证,要么提供身份证,而且不是“数字身份证明”。检查$_GET['id']
。
答案 1 :(得分:0)
让您的网址= http://www.domain.com?id= [YOUR_IMAGE_ID]
通过?id = your_id 查询字符串,您将获得 $ _ GET ['id']
的ID删除标题(“Content-type:image / png”);
在 $ result 变量
下添加这两行$res1= mysqli_result($result, 0);
echo '<img src="data:image/png;base64,' . base64_encode($res1) . '" />';
我也无法在上面的代码中找到 mysqli_result 函数。如果你还没有宣布
请使用此 -
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
答案 2 :(得分:0)
这是完整的代码 -
CREATE TABLE IF NOT EXISTS `tbl_images` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
PHP
<?php
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
# Connect to DB
$link = mysqli_connect("$host", "$username", "$password") or die("Could not connect: " . mysqli_error());
mysqli_select_db("$database") or die(mysqli_error());
# SQL Statement
$sql = "SELECT `image` FROM `tbl_images` WHERE id=" . mysqli_real_escape_string($_GET['id']) . ";";
$result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());
# Set header
$res1= mysqli_result($result, 0);
echo '<img src="data:image/png;base64,' . base64_encode($res1) . '" />';
} else
echo 'Please check the ID!';
?>