0
<?php
header("Content-type: image/jpeg");

$id = $_GET["ident"];
$pat = '/^[0-9]+$/';
if(!preg_match($pat, $id)){
    exit; // broken image
}

// Using prepared statement, and bind-result don't work
// too well for a blob. PHP tries to allocate 16Mbye for the blob!
$mysqli = new mysqli('localhost','root','9876543210','student13');
$query = "select image from Picys where ident=$id";

$result = $mysqli->query($query);

$err = $mysqli->error;
if(!empty($err)){
    exit;
}

$row = $result->fetch_array(MYSQLI_NUM);
$bytes = $row[0];

echo $bytes;


?>

这就是我从 SQL 服务器获取图片的方式

但是当我调用这个 ImagefromMySQL.php 时它不起作用

<!DOCTYPE html>

<head>
    <title>Display Picture</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h1>A Picture from Our Collection</h1>
    <?php 
    $id = $_GET["ident"];
    echo "<img src='./ImageFromMySQL.php?id=$id' />"
    ?>
    <hr />
    <h2>Tags</h2>
    <?php

    $stmt->close();
    $stmt = $mysqli->prepare("select Tagstr from PicyTags where Picid=?");
    $stmt->bind_param('s',$id);
    $stmt->execute();
    $stmt->bind_result($tag);
    $count = 0;
    while($stmt->fetch()){
        if($count==0){
            echo "<bold>Existing tags:</bold>";
        }
        $count++;
        echo "$tag<br />";
    }
    $mysqli->close();
    ?>
    <br />
    <h3>Add tag</h3>
    <p>You may add tags that characterize the contents of this picture.</p>
    <form action="./AddTag.php?ident=<?php echo $id ?>" method="POST">
        <fieldset>
            <legend>Your tag</legend>
                <input type="text" size="16" maxlength="16" />
        </fieldset>
        <input type="submit" value="Add Tag" />
    </form>
</body>

我的 SQL 数据库看起来像这个 ident 标题评论图像 6 sds sdsds BLOB - 80.5KiB 1 aaa sss BLOB - 123.6KiB

4

1 回答 1

0

问题是您的 PHP 代码正在执行:$_GET["ident"];但您的 HTML 正在执行:?id=$id。您需要更改$_GET['indent']$_GET['id']或更改?id=?ident=

调试问题的另一种方法是直接在浏览器中打开图像,它会显示发生的 PHP 错误,而不是创建图像。您可能还需要注释掉内容类型标头以查看错误。

于 2013-06-09T05:23:57.643 回答