如何以特殊日期显示存储在Mysql数据库中的图像而不是按ID(PHP)

塞利姆·卡拉卡(Selim Karaca)

我正在尝试显示上传到MySql中“上传”表的图像。我已经读了很多关于如何做到这一点的书,但是没有运气。

“ news_content”表用于将NEWS内容上载到Mysql,并具有6列:id,标题,描述,content_text,日期,时间

和“上传”表有5列:ID,名称,大小,图像,日期

在“ news_content”表中,我分别上传了日期和时间列,但“上载”表中的日期列是一个字符串,其中同时包含了日期和时间。例如,如果在“ news_content”表中日期为2/3/2016,时间为5:30,则在“上载”表中日期为2/3/20165:30。我以这种方式进行了组织,以便按相关帖子上传的特定日期和时间来检索图像。

使用以下代码将图像上传到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数据:

PS图像已成功上传,但我无法按最近发布的特定日期进行检索

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'];

我想使用以下HTML代码index.php页面中显示图像

index.php:

<img src="getImage.php?date=<?php echo $filedate ; ?>" width="175" height="200" />

如何在getImage.php页面中检索该数据,然后使用该页面和az源页面在index.php页面中显示图像任何帮助,将不胜感激。

兰霍特

除非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" />

然后我的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'];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在MySQL数据库中存储和显示图像

如何在MySQL中按DMY格式从Mysql数据库中按日期列显示并基于它进行搜索

如何在php页面上显示存储在数据库中的图像?

无法在 PHP 中显示 MySQL 数据库中的图像

PHP MySQL显示具有正确ID的数据库中的图像

如何使用php显示来自数据库mysql的图像?

如何将图像文件夹路径存储到 MYSQL 数据库中并通过 PHP 和 XAMPP 在网页中显示图像?

如何使用php将图像存储在mysql数据库中

如何在数据库php / mysql中存储图像链接

PHP MySQL从数据库显示图像

如何以与存储在数据库中相同的格式显示数据?

如何显示按日期排序和按日期分组的数据库中的JSON记录

如何从数据库MYSQLi在php中显示图像?

如何使用 php 显示 MySQL 数据库中的十进制和日期数组

如何直接从mysql数据库中获取图像并使用php以json格式显示它们?

在php中从数据库显示图像

如何执行存储在MySQL数据库中的PHP函数

如何使用PHP在MySQL数据库中输入日期

如何使用 php 和 mysql 从网站上抓取日期并将该日期存储在数据库中?

我想从 mysql 数据库中检索图像,但它显示的是字符而不是图像

通过PHP检索存储在MySQL数据库中的本地主机上的本地主机上的图像显示

如何从PHP中的MySQL数据库获取图像

如何在存储在 SQL Server 数据库中的图像框中显示图像?

如何显示按日期 $row 中的日期排序的数据库结果

mysql显示数据库按创建日期排序?

显示所有存储为MySQL数据库中BLOB数据的图像

如何使用jQuery显示数据库中的图像而不是图像路径

如何以特定顺序显示数据库中php中的记录

如何以全日历显示数据库中的信息