2012-01-06 122 views
0

我有一个while循环从查询调用$ resultMysql查询While while循环内的错误

这里面while循环我有其他两个查询$ anchors1$ anchors2

第一个检索前两排;

第二个应该检索后面的使用偏移量

由于某些原因,查询似乎互相影响,不显示3行并拉出不应该在那里的重复行。

有什么办法可以解决这个问题吗? 如果我删除第一个,则第二个查询将起作用。反之亦然。

该平台是Wordpress。

while($slice = mysql_fetch_assoc($result)){ 

     $i++; 

    if($enable_position1 == 'y' && $i == $position[0]): 

     $anchors1 = mysql_query("SELECT * FROM anchors WHERE site_url = '$site_current' LIMIT 3"); 
     while($anc = mysql_fetch_assoc($anchors)){ 
     $site_anchor = $anc['site_anchor']; 
     $site_current = $anc['site_current']; 
     echo '<li><a href="'.$site_current.'" title="'.$site_anchor.'" target="_self">'.$site_anchor.'</a></li>'; 
     } 

    elseif($enable_position2 == 'y' && $i == $position[1]): 

     $anchors2 = mysql_query("SELECT * FROM anchors WHERE site_url = '$site_current' LIMIT 999 OFFSET 3"); 
     while($anc2 = mysql_fetch_assoc($anchors2)){ 
     $site_anchor2 = $anc2['site_anchor']; 
     $site_current2 = $anc2['site_current']; 
     echo '<li><a href="'.$site_current2.'" title="'.$site_anchor2.'" target="_self">'.$site_anchor2.'</a></li>'; 
     } 

    else: 

     the_post(); 

    endif; 

} 

非常感谢!

+0

在您的第二个查询中,当然'OFFSET 3'会导致无限结果集中的第三行作为有限结果集的第一行返回。你不需要把它改成'OFFSET 4'吗? – Atonewell 2012-01-06 22:17:27

回答

0

在第二个查询中,您正在使用变量$site_current,该变量在第一个查询的块中设置。根据应用程序的设计方式,这可能会导致干扰。我想你的意思是把$site_current2放在那里。

+0

That worked,ty :)这很奇怪,但$ site_current具有相同的值(这是一个多平台应用程序)。我做的是$ site_current2 = $ site_current;并应用您提出的更改。为什么会干涉? – webmasters 2012-01-06 22:24:41

+0

可能是因为你在第一次循环的每一次迭代中重写变量。 '$ site_current'是来自应用程序的其他部分吗?你应该避免重写变量,它们会导致像这样的错误。 – DfKimera 2012-01-06 22:38:26