简体   繁体   English

PHP 中的递归导航 function(mssql -> sqlsrv 问题)

[英]Recursive navigation function in PHP (mssql -> sqlsrv problem)

Our site has a function that grabs all of the navigation items and the sub navigation items as follows:我们的网站有一个 function ,它抓取所有导航项和子导航项,如下所示:

function setNav($sectionID = 0) {
  $divs = $db->getData("SELECT * 
                          FROM table 
                         WHERE Section = ".$sectionID);
  foreach ($divs as $div) {
    $subArray = setNav($div['ID']);
    $thisArray[$div['ID']]['ID'] = $div['ID'];
    $thisArray[$div['ID']]['Title'] = $div['Title'];
    $thisArray[$div['ID']]['Children'] = $subArray;
  }

  return $thisArray;
}

It worked fine when we used the mssql_ functions, but since switching to a new version of PHP where that is depreciated, it's throwing up an error saying:当我们使用 mssql_ 函数时它工作得很好,但是自从切换到 PHP 的新版本后,它会抛出一个错误说:

Fatal error: Maximum function nesting致命错误:最大 function 嵌套
level of '100' reached, aborting!达到'100'的水平,中止!

That message is coming from PHP, because you recursed into setNav too many times.该消息来自 PHP,因为您递归到 setNav 的次数太多。

Unless you've got a navigation tree that ends up a hundred entries deep, I suspect something might be going wrong with your call to getData.除非您的导航树最终包含一百个条目,否则我怀疑您对 getData 的调用可能会出现问题。

Can you make sure that it's returning valid entries?你能确保它返回有效的条目吗?

Also, as a1ex07 says, you might be best using some kind of recursive query, rather than recursive calls to the non-recursive query, if possible.此外,正如 a1ex07 所说,如果可能的话,您最好使用某种递归查询,而不是递归调用非递归查询。

This limit is set by xdebug.此限制由 xdebug 设置。 Check your php.ini and comment out the line that loads the xdebug library and then restart your web server.检查您的 php.ini 并注释掉加载 xdebug 库的行,然后重新启动您的 web 服务器。

Beyond that, you may wish to refactor your code to use nested sets in the future.除此之外,您可能希望将来重构代码以使用嵌套集。

For SQL Server 2005 and higher you can write something like this:对于 SQL Server 2005 及更高版本,您可以编写如下内容:

$divs = $db->getData(" 
WITH tableCTE AS  
  (SELECT * 
    FROM table 
    WHERE Section = ".$sectionID. "
    UNION ALL 
    SELECT t2.* FROM tableCTE t1
    INNER JOIN table t2 ON (t2.id = t1.Section)
   )
 SELECT * FROM tableCTE
  "
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM