我正在尝试显示上传到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] 删除。
我来说两句