php - 使用一个查询和一个 php 文件显示两个不同表中的图像

标签 php mysql sql

我在 loadimage.php 文件的一个查询中有两个选择语句,并以两个不同的过程显示它,一张用于事件的图像,一张用于新闻的图像。 我的代码是这样的

加载图像.php

<?php
mysql_connect("localhost","root");
mysql_select_db("pcnl");
$q="select * from tbl_event where event_ID = ".$_GET['id'];
$rec=mysql_fetch_array(mysql_query($q));
$image=$rec['event_img'];
header('Content-Length: '.strlen($image));
header("Content-type: image/".$rec['event_imgExt']);
echo $image;

$sqlmain="select * from tbl_news where news_ID = ".$_GET['mainid'];
$mainimg=mysql_fetch_array(mysql_query($sqlmain));
$mainimage=$mainimg['news_img'];
header('Content-Length: '.strlen($mainimage));
header("Content-type: image/".$mainimg['news_ext']);
echo $mainimage;
?>

事件.php

<img src="loadimage.php?id=<?php echo $events[id];?>" width="700px" height="350px">

新闻.php

<img src="loadimage.php?mainid=<?php echo $main['news_ID'];?>" width="300px" height="350px">

最佳答案

您没有提出任何问题,说了您期望发生什么,说了发生了什么,告诉我们运行此程序时应该记录的错误。

您无法返回多个资源来响应单个 HTTP 请求。 (这并不完全正确,但这就像在您仍在掌握算术基础知识时谈论弦理论一样)。

顺便说一句,您当前的脚本容易受到 SQL 注入(inject)攻击。

试试这个:

<?php
mysql_connect("localhost","root");
mysql_select_db("pcnl");
$t='event'==$_GET['itype'] ? 'tbl_event' : 'tbl_news';

$q="select * from $t where event_ID = ".(integer)$_GET['id'];
if ($rec=mysql_fetch_array(mysql_query($q))) {
   $image=$rec['event_img'];
   header('Content-Length: '.strlen($image));
   header("Content-type: image/".$rec['event_imgExt']);
   echo $image;
} else {
   header('HTTP/1.0 404 Not Found', 404);
}

还有...

<img src="loadimage.php?itype=event&id=<?php echo $events['id'];?>"...

<img src="loadimage.php?itype=main&id=<?php echo $main['news_ID'];?>"...

或者更好的是只有一个表保存数据。

关于php - 使用一个查询和一个 php 文件显示两个不同表中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640711/

相关文章:

php - mysqli 从不返回任何东西

sql - XML 到 SQL 的转换

mysql - 将另一个表导入数据库并添加字段

php - Icenium/phonegap ios 推送通知和 php 处理

php - 如何在 PHP 中有效地使用 try...catch block

php - 优化长时间运行的 SELECT DISTINCT 查询以及在查询中的何处使用 'EXPLAIN'?

ios - 是否需要检查 NSUserDefaults 和 SQL 表是否存在于手机上?

php - 将动态表单发布到数据库

mysql - 具有奇数一对多关系的高效 SQL 模式设计

android - 获取最近一小时更新的行