即使查询不匹配,Mysql结果也会重复

时间:2013-07-19 14:27:33

标签: php mysql

首先为我在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"];
}

非常感谢任何帮助。谢谢。

2 个答案:

答案 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 = '';

或者您将始终获得最后一个值