2012-04-19 69 views
1

我有3個獨立的MySQL查詢下面列出,有沒有結合他們這樣我就可以得到輸出相同,但只需一個電話到數據庫(而不是3)的MySQL調用數據庫減倍

由於任何方式提前

<?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的新手和我不能確定如何使任何或所有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>"; 
} 
?> 
+3

首先你不需要每次的mysql_connect的。其次,你實際上並沒有使用$ result的值。第三,'ORDER BY RAND()'速度非常慢。總而言之,你真的需要重新思考你想在這裏實現的目標。 – 2012-04-19 12:17:27

回答

1

如何使用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>'; 
} 
+0

原諒我的無知,但我的'打印'「一個href = somelink>圖像」; }條件進入它? - 只有在數據庫中匹配「extra1」或「extra5」的值時,我才希望MySQL顯示圖像 – 2012-04-19 18:16:31

+0

@JasonSneyd:請參閱我的編輯。 – Toto 2012-04-20 08:09:15

+0

謝謝,這很好!感謝你的幫助! – 2012-04-20 20:05:17

1
SELECT id,cat,extra1,bigimage 
FROM pp_photos 
WHERE extra1 IN('W599_PTO','120','120rear') 
GROUP BY extra1 

這會給你3行。您需要過濾根據某些條件得到3行所需的行,爲了通過不需要

+0

感謝您的回覆!雖然這有效,但它沒有做它應該做的。基本上,如果有與W599_PTO相匹配的照片,則顯示圖像,'120和'120rear'也是如此 - 如果沒有與任何照片匹配的照片,則不顯示任何圖像。用上面的答案,無論如何顯示所有圖像。謝謝 – 2012-04-19 12:25:38