如何显示存储在 Mysql 数据库中的具有特殊日期而不是 id (php) 的图像
How to display an image stored in Mysql database with special date not by id (php)
我正在尝试在 MySql 中显示上传到我的 "upload" table 的图像。我已经阅读了很多有关如何执行此操作的信息,但没有运气。
"news_content" table 用于将新闻内容上传到 Mysql 并且有 6 列:id,title, description, content_text, date, time
和"upload" table有5列:id, name, size, image, date
在"news_content" table中,我分别上传了日期和时间列,但"upload" table中的日期列是一个与日期和时间连接的字符串。例如,如果在 "news_content" table 中日期是 2/3/2016 并且时间是 5:30,在 "upload" table 中日期将是 2 /3/20165:30。我以这种方式组织它,以便按相关 post 上传的特定日期和时间检索图像。
我使用以下代码在 news.php 页面上传图片:
news.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//uploading the content of news and date and time
if(isset($_POST["submit"])){
$title = $_POST["title"] ;
$description = $_POST["description"] ;
$content_text = $_POST["content_text"] ;
$date = $_POST["date"] ;
$time = $_POST["time"] ;
//perform mysql query
$result = mysql_query("INSERT INTO news_content (title, description, content_text, date, time)
VALUES ('$title', '$description', '$content_text' ,'$date' , '$time' )") ;
if (!$result) {
die("Insert failed: " . mysql_error());
$submitMessage = "Problem with updating the post, please try again" ;
} else if ($result){
$submitMessage = "Your post succsessfully updated" ;
}
}
// uploading the image
if(isset($_POST['submit']) && $_FILES['image']['size'] > 0)
{
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
$filedate = "$date" . "$time" ;
$fp = fopen($tmpName, 'r');
$content2 = fread($fp, filesize($tmpName));
$content3 = addslashes($content2);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, image, date ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content3', '$filedate')";
mysql_query($query) or die('Error2, query failed');
}
我想通过 getImage.php 页面检索该图像,稍后通过以下代码将其用作源页面,但它似乎无法检索 blob数据:
P.S。图片已成功上传,但我无法用我最近发布的具体日期检索它
getImage.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//perform mysql query
$result = mysql_query("SELECT * FROM news_content" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
}
//useing returned data
while ($content = mysql_fetch_array($result)){
$date = $content["date"];
$time = $content["time"] ;
}
$filedate = "$date" . "$time" ;
$result2 = mysql_query("SELECT image FROM upload WHERE date='$filedate'" , $connection);
if (!$result2) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result2);
mysql_close($connection);
header("Content-type: image/jpeg");
echo $row['image'];
我想在 index.php 页面中使用以下 HTML 代码显示图像:
index.php :
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
如何在 getImage.php 页面中检索该数据,然后使用该页面 az 源页面在 [=68= 中显示图像]页?
任何帮助将不胜感激。
显示错误:
在页面顶部添加 error_reporting(E_ALL); ini_set('display_errors', '1');
;
暂时注释header行:header("Content-type: image/jpeg");
;
直接从浏览器地址栏获取图片(见下图);
如果您收到“500 服务器错误”,请检查您的 Apache 错误日志(即使启用 error_reporting 也不会显示编译错误);
无法找出错误,除非 php 抛出错误。但我会将图像保存为文件而不是数据库,并在新闻 table 的行中保留其名称,除非每个新闻有多个图像。
如果每个新闻有多个图片,我只会将图片 ID 与新闻 ID 匹配。
news.db
的架构
id, title, description, content_text, date, time
upload.db 的架构:
id, image, newsid
我的图片 html link 应该是:
<img src="getImage.php?id=<?php echo $newsid; ?>" width="175" height="200" />
然后我的 getimage.php 会是这样的:
$connection = mysql_connect("localhost","root","p206405az");
if (!$connection) {
die("Connection failed: " . mysql_error());
}
$db_select = mysql_select_db( "images" , $connection) ;
if (!$db_select) {
die("Selection faild:" . mysql_error()) ;
}
$id=$_GET["id"];
$result = mysql_query("SELECT image FROM upload WHERE newsid='$id' LIMIT 1" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result);
mysql_close($connection);
header("Content-type: image/jpeg");
print $row['image'];
我正在尝试在 MySql 中显示上传到我的 "upload" table 的图像。我已经阅读了很多有关如何执行此操作的信息,但没有运气。
"news_content" table 用于将新闻内容上传到 Mysql 并且有 6 列:id,title, description, content_text, date, time
和"upload" table有5列:id, name, size, image, date
在"news_content" table中,我分别上传了日期和时间列,但"upload" table中的日期列是一个与日期和时间连接的字符串。例如,如果在 "news_content" table 中日期是 2/3/2016 并且时间是 5:30,在 "upload" table 中日期将是 2 /3/20165:30。我以这种方式组织它,以便按相关 post 上传的特定日期和时间检索图像。
我使用以下代码在 news.php 页面上传图片:
news.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//uploading the content of news and date and time
if(isset($_POST["submit"])){
$title = $_POST["title"] ;
$description = $_POST["description"] ;
$content_text = $_POST["content_text"] ;
$date = $_POST["date"] ;
$time = $_POST["time"] ;
//perform mysql query
$result = mysql_query("INSERT INTO news_content (title, description, content_text, date, time)
VALUES ('$title', '$description', '$content_text' ,'$date' , '$time' )") ;
if (!$result) {
die("Insert failed: " . mysql_error());
$submitMessage = "Problem with updating the post, please try again" ;
} else if ($result){
$submitMessage = "Your post succsessfully updated" ;
}
}
// uploading the image
if(isset($_POST['submit']) && $_FILES['image']['size'] > 0)
{
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
$filedate = "$date" . "$time" ;
$fp = fopen($tmpName, 'r');
$content2 = fread($fp, filesize($tmpName));
$content3 = addslashes($content2);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$query = "INSERT INTO upload (name, size, type, image, date ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content3', '$filedate')";
mysql_query($query) or die('Error2, query failed');
}
我想通过 getImage.php 页面检索该图像,稍后通过以下代码将其用作源页面,但它似乎无法检索 blob数据:
P.S。图片已成功上传,但我无法用我最近发布的具体日期检索它
getImage.php :
// Create connection
$connection = mysql_connect("localhost","root","p206405az");
// Check connection
if (!$connection) {
die("Connection failed: " . mysql_error());
}
//select a database to use
$db_select = mysql_select_db( "news" , $connection) ;
if (!$db_select) { die("Selection faild:" . mysql_error()) ;
}
//perform mysql query
$result = mysql_query("SELECT * FROM news_content" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
}
//useing returned data
while ($content = mysql_fetch_array($result)){
$date = $content["date"];
$time = $content["time"] ;
}
$filedate = "$date" . "$time" ;
$result2 = mysql_query("SELECT image FROM upload WHERE date='$filedate'" , $connection);
if (!$result2) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result2);
mysql_close($connection);
header("Content-type: image/jpeg");
echo $row['image'];
我想在 index.php 页面中使用以下 HTML 代码显示图像:
index.php :
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
如何在 getImage.php 页面中检索该数据,然后使用该页面 az 源页面在 [=68= 中显示图像]页? 任何帮助将不胜感激。
显示错误:
在页面顶部添加
error_reporting(E_ALL); ini_set('display_errors', '1');
;暂时注释header行:
header("Content-type: image/jpeg");
;直接从浏览器地址栏获取图片(见下图);
如果您收到“500 服务器错误”,请检查您的 Apache 错误日志(即使启用 error_reporting 也不会显示编译错误);
无法找出错误,除非 php 抛出错误。但我会将图像保存为文件而不是数据库,并在新闻 table 的行中保留其名称,除非每个新闻有多个图像。
如果每个新闻有多个图片,我只会将图片 ID 与新闻 ID 匹配。
news.db
的架构id, title, description, content_text, date, time
upload.db 的架构:
id, image, newsid
我的图片 html link 应该是:
<img src="getImage.php?id=<?php echo $newsid; ?>" width="175" height="200" />
然后我的 getimage.php 会是这样的:
$connection = mysql_connect("localhost","root","p206405az");
if (!$connection) {
die("Connection failed: " . mysql_error());
}
$db_select = mysql_select_db( "images" , $connection) ;
if (!$db_select) {
die("Selection faild:" . mysql_error()) ;
}
$id=$_GET["id"];
$result = mysql_query("SELECT image FROM upload WHERE newsid='$id' LIMIT 1" , $connection);
if (!$result) {
die("read failed: " . mysql_error());
};
$row = mysql_fetch_assoc($result);
mysql_close($connection);
header("Content-type: image/jpeg");
print $row['image'];