-1
我正面临从mysql数据库获取图像并显示在网页上的问题。 我有一个user_image表与用户ID,图像(BLOB数据类型),标题,图像名称(图像的名称,例如0101.jpg),时间(图像上传的时间戳)。 用户可以将图像上传到数据库,并根据时间戳查看他们的图像。 关于如何在网页上显示图像的任何输入? 下面是代码:获取BLOB图像并使用php在html中显示使用php
<?php
session_start();
$con = mysqli_connect("localhost", "root", "","login") or die("Unable to connect to MySQL");
$userid = $_SESSION['userid'];
$sql = "SELECT image,image_name,caption FROM user_image WHERE userid=$userid";
if($query = mysqli_query($con, $sql)) {
while($row=mysqli_fetch_array($query)) {
header('Content-type: image/jpeg');
echo $row["image"];
}
}else{
echo "Error";
}
$con->close();
?>
下面是图片上传到数据库的代码:
<?php
session_start();
if (isset($_POST['submit'])){
echo $_SESSION["userid"];
$userid = $_SESSION["userid"];
$con=mysqli_connect("localhost","root","","login")or die("Unable to connect to MySQL");
$caption = $_POST['Caption'];
$imageName = mysqli_real_escape_string($con,$_FILES["fileToUpload"]["name"]);
$imageData = mysqli_real_escape_string($con,file_get_contents($_FILES["fileToUpload"]["tmp_name"]));
$imageType = mysqli_real_escape_string($con,$_FILES["fileToUpload"]["type"]);
echo $imageName;
echo $imageType;
if(substr($imageType,0,5) == "image"){
$stmt = mysqli_prepare($con, "INSERT INTO user_image (userid,timestamp, image, image_name,caption) VALUES (?,now(), ?, ?,?)");
if ($stmt === false) {
trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($con)), E_USER_ERROR);
}
$bind = mysqli_stmt_bind_param($stmt, "isss", $userid,$imageData, $imageName, $caption);
if ($bind === false) {
trigger_error('Bind param failed!', E_USER_ERROR);}
$exec = mysqli_stmt_execute($stmt);
if ($exec === false) {
trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR); }
}
else
{
echo " only images are allowed";}
}
$con->close();
?>
那么一些构成网页的HTML将是一个好开始 – RiggsFolly
欢迎来到SO。 请阅读[我可以问哪些主题](http://stackoverflow.com/help/on-topic) 和[如何提出一个好问题](http://stackoverflow.com/help/how-to - 问) 和[完美的问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何创建[最小,完整和可验证的例子] (http://stackoverflow.com/help/mcve) SO是**不是一个免费的编码或代码转换或调试或教程或库查找服务** 您还必须表明,你已经努力解决你自己的问题。 – RiggsFolly
@RiggsFolly - 请你详细一点。我不明白你在问什么。 – Mathak