繁体   English   中英

从MySQL数据库中检索图像

[英]Retrieving images from MySQL database

我是php的新手,我正在尝试从我的MySQL数据库中检索图像。

运行以下脚本后,我会收到“请检查ID!” 信息。 我错过了什么吗?

数据库结构

CREATE TABLE tbl_images(
    id tinyint( 3 ) unsigned NOT NULL AUTO_INCREMENT ,
    image blob NOT NULL ,
    PRIMARY KEY ( id ) 
)

PHP

<?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!';
?>

因此,您要么不提供身份证,要么提供身份证,而且不是“数字身份证明”。 检查$_GET['id']

将您的网址设为= 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];
}

这是完整的代码 -

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!';
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM