首先为我在php / mysql中的可疑编码道歉但是这都是自学(可能不是最佳实践)
我的所有代码似乎都有效,但是当结果写入页面时,任何不在$dxcall
数据库中的$qrzdata
都会填充最后一个结果,所有其他数据都显示正常。我尝试将like $dxcall
更改为= $dxcall
。我也尝试过组合fetch数组,因为我的问题也存在。但显然我的代码不知道如何处理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"];
}
非常感谢任何帮助。谢谢。
答案 0 :(得分: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)
答案 1 :(得分:0)
你必须重置你的变种!
While ($r0 = mysql_fetch_array($frqry))
{
$qra = '';
$country = '';
$firstname = '';
$city = '';
或者您将始终获得最后一个值