簡體   English   中英

如何以特殊日期顯示存儲在Mysql數據庫中的圖像而不是按ID(PHP)

[英]How to display an image stored in Mysql database with special date not by id (php)

我正在嘗試顯示上傳到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頁面中顯示圖像? 任何幫助,將不勝感激。

顯示錯誤:

  1. 添加error_reporting(E_ALL); ini_set('display_errors', '1'); error_reporting(E_ALL); ini_set('display_errors', '1'); 在頁面頂部;

  2. 臨時注釋標題行: header("Content-type: image/jpeg"); ;

  3. 直接從瀏覽器地址欄中檢索圖像(請參見下圖);

  4. 如果獲得“ 500服務器錯誤”,請檢查您的Apache錯誤日志(即使啟用了error_reporting,也不顯示編譯錯誤);

在此輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM