如何显示存储在Mysql数据库中的具有特殊日期而不是id的图像(php)
我试图在MySql中显示上传到“上传”表的图像。我已经读了很多关于如何做到这一点的书,但是运气不好 “新闻内容”表用于将新闻内容上传到Mysql,共有6列:id、标题、描述、内容、文本、日期、时间 “上传”表有5列:id、名称、大小、图像、日期 在“news_content”表中,我分别上载日期和时间列,但“upload”表中的日期列是一个与日期和时间连在一起的字符串。例如,如果在“新闻内容”表中,日期为2016年2月3日,时间为5:30,则在“上传”表中,日期为20165:30。我以这种方式组织它,以便根据相关帖子上传的特定日期和时间检索图像 我使用以下代码在news.php页面上传图像: news.php:如何显示存储在Mysql数据库中的具有特殊日期而不是id的图像(php),php,mysql,image,blob,Php,Mysql,Image,Blob,我试图在MySql中显示上传到“上传”表的图像。我已经读了很多关于如何做到这一点的书,但是运气不好 “新闻内容”表用于将新闻内容上传到Mysql,共有6列:id、标题、描述、内容、文本、日期、时间 “上传”表有5列:id、名称、大小、图像、日期 在“news_content”表中,我分别上载日期和时间列,但“upload”表中的日期列是一个与日期和时间连在一起的字符串。例如,如果在“新闻内容”表中,日期为2016年2月3日,时间为5:30,则在“上传”表中,日期为20165:30。我以这种方式组
// 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');
}
// 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'];
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
我想通过getImage.php页面检索该图像,以便稍后通过以下代码将其用作源页面,但它似乎无法检索blob数据:
另外,图片已成功上传,但我无法检索到我最近发布的具体日期
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()) ;
}
//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');
}
// 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'];
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
我想用以下HTML代码在index.php页面中显示图像:
index.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');
}
// 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'];
<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />
“width=“175”height=“200”/
如何在getImage.php页面中检索该数据,然后使用该页面和源页面在index.php页面中显示该图像?
任何帮助都将不胜感激。要显示错误:
error\u reporting(E\u ALL);ini\u set('display\u errors','1');
标题(“内容类型:image/jpeg”);
除非php抛出错误,否则无法找出错误。但我会将图像保存为文件,而不是db,并保留其名称(如果新闻表的行),除非每个新闻有多个图像 对于每个新闻的多示例图像,我只需将图像id与新闻id匹配 news.db的模式
id, title, description, content_text, date, time
upload.db的架构:
id, image, newsid
我的图像html链接应该是:
<img src="getImage.php?id=<?php echo $newsid; ?>" width="175" height="200" />
有什么错误?@fusion3k无。这就是问题所在。它只是不显示图像,而是显示一个损坏的图像标志。直接通过url(不在
标记中)和注释header()
命令获取图像以查看错误。@fusion3k sry你能给我一个示例吗?(在代码中)它只给我这个错误:图像”“无法显示,因为它包含错误。我应该将日期列设置为主键吗?1)使用斜杠作为分隔符不是一个好主意;2) 您是否对标题
行进行了注释?请在下面的行中添加注释,以查看文本是否在图像之前打印。请同时检查$filedate
变量的值:您确定它与日期
数据库字段匹配吗?如果date
字段是datetime格式,我认为它可能不匹配。另外:您需要一个特定的图像,但是在您的代码处理过程中,url传递的日期在哪里?这里$GET[“id]做什么?它是GET table的id吗?如果我想通过日期列而不是id获取另一行,我应该怎么做?另一个问题是,如果我在一个数据库中有3个表,那么哪一个表的id$\u GET[“id”]获取?第一个表或第二个表或第三个表的id?这是我在类似于您的场景中提出的解决方案。请参阅上面的图像html链接代码。它将从具有图像的数据库中请求特定图像(上载表)。然后getimage.php将返回该图像(来自上载表)以html显示。