如何使用 php 和 mysql 从多列检索和输出图像?
更新:
我的 SQL 表中有十列:id、imgid、urlid、image1、image2、image3、image4、image5 和 comment。 id、imgid、urlid 都是 int 类型。图像[1-5]是mediumblob类型。网址和评论都是文本类型。 imgid是上传的图片数量(应该是5),urlid是提交的url数量(现在应该是1),url保存url,comment保存用户评论。我想检索并输出连续图像列中的所有图像数据,但页面图标被撕裂。我使用 front.php 来输出图像,并使用 get.php 将图像转换为可查看的格式。
这是我的 front.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$defaultqry = mysql_query ("SELECT * FROM dbp");
while($row = mysql_fetch_assoc($defaultqry))
{
echo ($row['image1'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=1 " width="30" height="30"/> ' : '';
echo ($row['image2'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=2 " width="30" height="30"/> ' : '';
echo ($row['image3'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=3 " width="30" height="30"/> ' : '';
echo ($row['image4'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=4 " width="30" height="30"/> ' : '';
echo ($row['image5'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=5 " width="30" height="30"/> ' : '';
}
?>
这是我的 get.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$query = mysql_query("SELECT * FROM dbp WHERE id = ".mysql_real_escape_string($_GET['id']));
$row = mysql_fetch_array($query);
$col = intval($_GET['col']);
if(isset($row['image'.$col]) && $row['iamge'.$col] != NULL){
$content = $row['image'.$col];
}else{
exit; // col is not existent, or image is empty
}
header('Content-type: image/jpg');
echo $content;
exit;
?>
UPDATE:
I have have ten columns in my SQL table: id, imgid, urlid, image1, image2, image3, image4, image5, and comment. Id, imgid, and urlid are int type. Image[1-5] are mediumblob type. Url and comment are text type. Imgid is the number of images uploaded (which should be 5), urlid is the number of urls submitted (which should be one right now), url holds the url, and comment holds user comments. I'd like to retrieve and output all the image data from the image columns in a row but I'm getting torn page icons. I am using a front.php to output images and a get.php to convert images to viewable format.
Here's my front.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$defaultqry = mysql_query ("SELECT * FROM dbp");
while($row = mysql_fetch_assoc($defaultqry))
{
echo ($row['image1'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=1 " width="30" height="30"/> ' : '';
echo ($row['image2'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=2 " width="30" height="30"/> ' : '';
echo ($row['image3'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=3 " width="30" height="30"/> ' : '';
echo ($row['image4'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=4 " width="30" height="30"/> ' : '';
echo ($row['image5'] != NULL) ? '<img src="get.php?id='.$row[id].'&col=5 " width="30" height="30"/> ' : '';
}
?>
Here's my get.php:
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$query = mysql_query("SELECT * FROM dbp WHERE id = ".mysql_real_escape_string($_GET['id']));
$row = mysql_fetch_array($query);
$col = intval($_GET['col']);
if(isset($row['image'.$col]) && $row['iamge'.$col] != NULL){
$content = $row['image'.$col];
}else{
exit; // col is not existent, or image is empty
}
header('Content-type: image/jpg');
echo $content;
exit;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您使用数据库,在其中存储数据而不是文件。 (您应该将文件的链接/路径存储在数据库中,而不是图像本身,这只会破坏您的数据库并使其变慢)请参阅 在数据库中存储图像 - 是还是否? 有关此主题的更多信息。
但如果你想这样做,那么你将必须输出行的 id 和列标识符,例如:
在 get.php 中:
first, you use a DATAbase, store data in it not files. (you should store the link/path to the files in the DB not the images itself, this only blows up your database and makes it slow) see Storing Images in DB - Yea or Nay? for more info on this topic.
but if you want to do it this way then you will have to output the id of the row AND a column identifier like:
in your get.php: