2013-07-19 61 views
0

道歉,首先我在PHP可疑的編碼Mysql的結果/ MySQL的不過這是所有自學(可能不是最好的做法)複製即使查詢不匹配

所有我的代碼似乎工作,結果然而,當被寫入頁面的任何$dxcall那不在$qrzdata數據庫中被填滿最後的結果所有其他數據顯示正常。我曾嘗試將like $dxcall更改爲= $dxcall。我也嘗試了將提取數組結合起來,以免我的問題也在那裏。但顯然,我的代碼不知道如何處理qrzdata數據庫中沒有數據匹配的地方,並繼續前進。

$frqry是主要數據,所有其他mysql_query的是它的$squares$qrzdata匹配什麼來自$frqry。希望這是有道理的!

這裏是我的代碼

$frqry = mysql_query("select * from spots where freq between '69900' and '70300' ORDER BY datetime desc limit 30"); 
While ($r0 = mysql_fetch_array($frqry)) 
{ 
$freq = $r0["freq"]; 
$dxcall = $r0["dxcall"]; 
$datetime = $r0["datetime"]; 
$comments = $r0["comments"]; 
$spotter = $r0["spotter"]; 
$dt = date('d-m-y H:i ', $datetime); 
$qra = $r0["loc"]; 

$squares = mysql_query("select * from squares where callsign like '$dxcall' limit 1"); 
while ($r1 = mysql_fetch_array($squares)) 
{ 
$qra = $r1["loc"]; 
} 

$qrzdata = mysql_query("select * from qrzdata where callsign = '".$dxcall."' limit 1"); 
While ($r2 = mysql_fetch_array($qrzdata)) 
{ 
$country = $r2["country"]; 
$firstname = $r2["firstname"]; 
$city = $r2["city"]; 
} 

任何幫助是極大的讚賞。謝謝。

+1

什麼是那些點嵌套'while'如果您在查詢中使用'LIMIT 1'? – Eggplant

回答

2

你需要了解JOIN的功率;)

你的整個代碼可以在一個單一的查詢重寫:

免責聲明:沒有測試過,但你肯定有想法

SELECT * FROM spots 
JOIN squares ON (squares.callsign = spots.dxcall) -- this comes in stead of your first inner loop 
JOIN qrzdata ON (qrzdata.callsign = spots.dxcall) -- this is your second loop 
WHERE freq BETWEEN 69900 AND 70300 -- remove quotes, you are dealing with integers, not strings (hopefully) 
0

你必須重置你的變量!

While ($r0 = mysql_fetch_array($frqry)) 
{ 
    $qra = ''; 
    $country = ''; 
    $firstname = ''; 
    $city = ''; 

,否則您將得到百達的最後一個值