Mysql - “递归”不适用于 PHP

问题描述 投票:0回答:0

我正在尝试从数据库中按层次结构顺序打印类别。我开发了一个可以在 dbfiddle (https://dbfiddle.uk/_oUW-eW_) 中运行的 mysql 代码,但我无法让它在 PHP 中运行。

mysql代码如下:

with recursive cteBottomToTop
    AS (select id, name, title, parentid
        from $treedb
        where id = $start  
      union all
    select c.id, c.name, c.title, c.parentid
        from $treedb c
        inner join cteBottomToTop cte
          on c.id = cte.parentid)
    select *
    from cteBottomToTop

使用的PHP代码如下:

    $sqltree = ("with recursive cteBottomToTop
    AS (select id, name, title, parentid
        from $treedb
        where id = $start  
      union all
    select c.id, c.name, c.title, c.parentid
        from $treedb c
        inner join cteBottomToTop cte
          on c.id = cte.parentid)
    select *
    from cteBottomToTop");
    $result = mysql_query($sqltree);
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "n";
        $error = $error.$message;
    }
    $number = mysqli_num_rows($result);
    if ($number < 1) {
        $error = $error."<p>Problemi nel caricare dal DB_tree</p>";
        }else{
        while ($row = mysqli_fetch_array($result)) {
            $name_print[] = $row["name"];
            $title_print[] = $row["title"];
            $parentid_print[] = $row["parentid"];
        }
    }

代码返回以下错误:

  1. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given
  2. Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'recursive cteBottomToTop AS (select id, name, title, parentid from ' at line 1n

有什么帮助吗?

php mysql hierarchy recursive-cte
© www.soinside.com 2019 - 2024. All rights reserved.