- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可能没有在此处查询中的最佳查询,如果有人可以教我更好的方法,我将不胜感激。
但是根据我所拥有的,我似乎无法弄清楚为什么 LIMIT 命令不起作用?
我将缩略图限制为 26 个,但我得到了 38 个?
很奇怪。
如果有帮助的话,我可以说画廊表有 7 个已发布的画廊,因此会有 7 个 id 来自那里,我希望下一个查询将循环遍历照片表并返回带有id 来 self 上面查询的 7 个画廊,对吗?
如果有人能理解的话,这就是我的代码..
<?php
// Check if gallery is published
$query1 = "SELECT id,status FROM gallery WHERE status=1";
$result1 = mysql_query($query1) or die(mysql_error());
while($row1 = mysql_fetch_array( $result1 )) {
$gid = $row1['id'];
// now get the photo file names based on the above published gallery ids
$query2 = "SELECT id,uid,gid,image,origimage FROM photo WHERE gid='$gid' LIMIT 20";
$result2 = mysql_query($query2) or die(mysql_error());
while($row2 = mysql_fetch_array( $result2 )) {
?>
<div style="float:left;">
<a class="featureGrid" href="public-photo-user.html?uid=<?php echo $row2['uid']; ?>&gid=<?php echo $row2['gid']; ?>&id=<?php echo $row2['id'];?>">
<img src="media/users/croppedthumbs/<?php echo $row2['uid']; ?>/<?php echo $row2['gid'] ?>/<?php echo $row2['image']; ?>" />
</a>
</div>
<?php }} ?>
感谢任何可以帮助阐明这一点或建议/教我一种更好的方法的人。
干杯。约翰
最佳答案
您正在执行多个查询,每个查询单独限制为 20 个。
相反,使用联接将查询合并为单个查询。它比循环多个查询更有效,并且您可以对组合结果进行限制:
SELECT photo.id, photo.uid, photo.gid, photo.image, photo.origimage
FROM photo JOIN gallery ON gallery.id=photo.gid
WHERE gallery.status=1
LIMIT 20
在将值放入查询字符串时,您还应该始终小心使用mysql_real_escape_string()
,否则您将遇到 SQL 注入(inject)安全漏洞。同样,输出到 HTML 页面的所有文本都必须使用 htmlspecialchars()
进行编码,以避免标记注入(inject),插入 URL 部分的数据应使用 rawurlencode()
进行编码>.
预计到达时间:
Can you link me to an example of mysql_real_escape_string combined with htmlspecialchars.
好吧,如果你还在这样做,那么:
$query2 = "SELECT id,uid,gid,image,origimage FROM photo WHERE gid='$gid' LIMIT 20";
需要对其中的 $gid
进行转义,否则值中的任何撇号(或可能的反斜杠)都会导致其爆炸。
$query2 = "SELECT id,uid,gid,image,origimage FROM photo WHERE gid='".mysql_real_escape_string($gid)."' LIMIT 20";
然后:
href="public-photo-user.html?uid=<?php echo $row2['uid']; ?>...
如果 uid
值包含双引号,则容易受到攻击,并且对于无法放入 URL 的其他各种字符也会失败。
也许您可以确保您的 ID 永远不会包含标点符号,但任何其他值都可能包含标点符号,因此每次将文本字符串插入另一个上下文(例如 SQL、HTML 或 URL)时,最好始终使用适当的编码。
不过,一直输入 mysql_real_escape_string
和 htmlspecialchars
有点乏味,所以我倾向于定义快捷函数,例如:
function m($str) { return "'".mysql_real_escape_string($str)."'"; }
function h($str) { echo htmlspecialchars($str); }
function u($str) { echo rawurlencode($str); }
可以这样使用:
$query2 = "SELECT id,uid,gid,image,origimage FROM photo WHERE gid=".m($gid)." LIMIT 20";
Hello, <?php h($name); ?>
<a class="featureGrid" href="public-photo-user.html?uid=<?php u($row2['uid']); ?>&gid=<?php u($row2['gid']); ?>&id=<?php u($row2['id']); ?>">
(另请注意,属性值中的 &
应转义为 &
,以保证 HTML 的有效性和可靠性。)
关于php - WHERE gid=$gid LIMIT 26 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926969/
我是一名优秀的程序员,十分优秀!