[?]"; -6ren">
gpt4 book ai didi

php - MySQL调用数据库次数少

转载 作者:行者123 更新时间:2023-11-29 07:00:52 24 4
gpt4 key购买 nike

我在下面列出了 3 个单独的 MySQL 查询,有没有什么方法可以将它们组合起来,这样我可以获得相同的输出,但只需调用一次数据库(而不是调用 3 个)

提前致谢

    <?php  
$host = '';
$dbUser = '';
$dbPass = '';
$db = '';
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120' ORDER BY rand() LIMIT 1" ) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
print "<a href=\"http://www.\" target=\"_new\"> <img src=\"ng\" border=\"0\" ></a>";
}
?>
<?php
$host = '';
$dbUser = '';
$dbPass = '';
$db = '';
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = 'W599_PTO' ORDER BY rand() LIMIT 1" ) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
print "<a href=\"php">[?]</FONT></a>";
}
?>
<?php
$host = '';
$dbUser = '';
$dbPass = '';
$db = '';
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("SELECT id,cat,extra1,extra5,bigimage FROM pp_photos WHERE extra5 = '120rear' ORDER BY rand() LIMIT 1" ) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
print "<a href=\"09\" target=\"_new\"> <img src=\"http://pg\" border=\"0\" ert\"></a>";
}
?>

我现在已经根据下面提供的帮助编辑了代码...我是 MySQL 的新手,我不确定如何设置过滤器,以便根据 'extra' 的值显示任何或所有 3 个图像1' 字段...

    <?php  
$host = '';
$dbUser = '';
$dbPass = '';
$db = '';
mysql_connect("$host", "$dbUser", "$dbPass") or die(mysql_error());
mysql_select_db("$db") or die(mysql_error());

$result = mysql_query("
SELECT extra1
FROM pp_photos
WHERE extra1 IN('1','10','12')
GROUP BY extra1" )
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
print "<a href=\"h3\" target=\"_new\"> images</a>";
}
?>

最佳答案

如何使用 UNION

$query = "
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120' ORDER BY rand() LIMIT 1
UNION
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = 'W599_PTO' ORDER BY rand() LIMIT 1
UNION
SELECT id,cat,extra1,bigimage FROM pp_photos WHERE extra1 = '120rear' ORDER BY rand() LIMIT 1
";
$cats = array(
'120' => '503',
'W599_PTO' => '508',
'120rear' => '509'
);
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
print '<a href="http://www.nctfleetlist.co.uk/photos/search.php?keywords='.
$row['extra1'] .
'&cat=' .
$cats[$row['extra1']] .
'" target="_new"><img src="http://www.nctfleetlist.co.uk/images/camera.png" border="0" title="photos available"></a>';
}

关于php - MySQL调用数据库次数少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com