![](/img/trans.png)
[英]how to display blob type images stored in MySql database on a jquerymobile template
[英]Display all images stored as BLOB data from MySQL database
我已經使用PHP將一些圖像上傳到MySQL。 每當我修改HTML img標簽中的ID時,我也可以顯示1張圖片。 但是現在我試圖顯示存儲在MySql數據庫中的所有圖像,問題是當我使用“ While Loop”時,它僅顯示文本列,而不顯示為BLOB數據存儲在MySQL中的圖像...
我有一個數據庫: my_db我有一個表: blob而且我的表中有3列: id , name和image
這是index.php的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"><input type="submit" name="submit" value="Upload">
</form>
<?php
if(isset($_POST['submit']))
{
mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image")
{
mysql_query("INSERT INTO `blob` VALUES('','$imageName','$imageData')");
echo "Image Uploaded!";
}
else
{
echo "Only images are allowed!";
}
}
?>
<img src="showimage.php?id=11">
</body>
</html>
這是showimage.php的代碼:
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");
if(isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");
while($row = mysql_fetch_assoc($query))
{
$imageData = $row["image"];
}
header("content-type: image/jpeg");
echo $imageData;
}
else
{
echo "Error!";
}
?>
提前致謝 :-)
首先,您需要刪除if(isset($_GET['id'])
語句,因為您想顯示所有圖像。
接下來,通過將查詢更改為不帶ID的查詢來查詢所有圖像
$query = mysql_query("select * from `blob`");
接下來,將圖像數據存儲到數組中。
$images = array();
while ($row = mysql_fetch_assoc($query)) {
$images[] = $row['image'];
}
最后,顯示所有圖像。 (從此處獲得代碼以顯示多個斑點)
/* header should be removed
header("content-type: image/jpeg"); */
foreach ($images as $image) {
echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}
在控制器中:
function display()
{
$imagename= mysql_real_escape_string($_FILES["userfile"]["name"]);
$imagedata= mysql_real_escape_string(file_get_contents($_FILES["userfile"]["tmp_name"]));
$imagetype= mysql_real_escape_string($_FILES["userfile"]["type"]);
$imagesize= mysql_real_escape_string($_FILES["userfile"]["size"]);
$imagedetails = array( 'image_name' => $imagename,
'image_data' => $imagedata
);
// $insertedid =$this->model_data->insert_image($imagedetails);
$this->load->model('model_data');
$data['imagee'] = $this->model_data->display_pic($insertedid); // variable data holds image
$this->load->view('profile_view',$data);
}
在模型中:
function display_pic($insertedid)
{
$query = mysql_query("SELECT * FROM employee_image WHERE id = ".$insertedid);
$row = mysql_fetch_assoc($query);
$this->db->stop_cache();
return $row['image_data'];
}
function insert_image($imagedetails) {
$this->db->insert($this->table_name2,$imagedetails);
$id=$this->db->insert_id();
return $id;
}
為什么當我刪除base64_encode時,此代碼未在codeigniter中運行,所以此代碼運行得非常完美,在file_get_contents中也是如此,為什么兩個函數均未在codeigniter中運行
$imagedata= mysql_real_escape_string(base64_encode(file_get_contents($_FILES["userfile"]["tmp_name"])));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.