Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何递归地获得;家长ID“;此MySQL表中的行数?_Php_Mysql_Recursion - Fatal编程技术网

Php 我如何递归地获得;家长ID“;此MySQL表中的行数?

Php 我如何递归地获得;家长ID“;此MySQL表中的行数?,php,mysql,recursion,Php,Mysql,Recursion,我的数据库看起来像(pligg cms,示例数据) 比如说,我如何获得芝加哥的顶级家长ID,应该是位置 我有没有用php编写递归函数,或者这在mysql中是可行的?网站对mysql和php中存储分层数据的不同方法有一个非常好的概述。要回答您的问题,最简单的方法是使用php和递归。您还可以使用其他方法,例如修改的preorder transversal,它不需要多个数据库查询。但在处理大量插入和更新时,此方法的实现可能更复杂 另一个非常酷的方法,也是我个人最喜欢的方法是在中提到的所谓的“闭包表”/

我的数据库看起来像(pligg cms,示例数据)

比如说,我如何获得芝加哥的顶级家长ID,应该是位置

我有没有用php编写递归函数,或者这在mysql中是可行的?

网站对mysql和php中存储分层数据的不同方法有一个非常好的概述。要回答您的问题,最简单的方法是使用php和递归。您还可以使用其他方法,例如
修改的preorder transversal
,它不需要多个数据库查询。但在处理大量插入和更新时,此方法的实现可能更复杂

另一个非常酷的方法,也是我个人最喜欢的方法是在中提到的所谓的“闭包表”/“邻接关系”

关于您的注释,您基本上必须创建一个循环或递归函数来选择chicago的父对象,然后选择父对象的父对象,依此类推

$stack = array();
$parent = 3;
while($parent != 0){
    $data = (put your mysql to get the row with parentID = $parent)
    $parent = data['parentID'];
    $stack[] = $data;
}

$stack = array_reverse($stack);

堆栈将包含Chicago(即location,USA)的父对象

因为mysql还不支持诸如connect by(oracle)或common table expressions(sql server)之类的功能。您将遇到的大多数示例都需要从php多次调用mysql,在层次结构中,每级调用一次。如果您有多个级别的树和大量并发用户,这很快就会累加起来。例如,一个深度为20的树和1000个并发用户将需要对数据库进行20K调用。以下方法使用存储过程,对于相同的场景,只需要1000个调用(每个用户1个)。如何处理resultset取决于您:您可以生成一个XML DOM,将其加载到数组中,或者只是将其输出为HTML,重要的是,您可以在一次调用中将整个树保存在resultset中

编辑:添加了一个category_父存储过程,该存储过程最初误读了您的问题

示例mysql调用

call category_hier(1);
call category_hier(2);

call category_parent(5);
call category_parent(7);
php脚本示例

<?php

$conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);

$result = $conn->query(sprintf("call category_parent(%d)", 5));
$row = $result->fetch_assoc();
$result->close();
$conn->close();

echo sprintf("parent category is : cat_id = %s category_name = %s",
 $row["cat_id"], $row["category_name"]);

?>

<?php

$conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306);

$result = $conn->query(sprintf("call category_hier(%d)", 1));

echo "<table border='1'>
        <tr><th>cat_id</th><th>category_name</th><th>parent_cat_id</th>
        <th>parent_category_name</th><th>depth</th></tr>";

while($row = $result->fetch_assoc()){
    echo sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", 
        $row["cat_id"],$row["category_name"],$row["parent_cat_id"],
        $row["parent_category_name"],$row["depth"]);
}

echo "</table>";

$result->close();
$conn->close();

?>
希望这有帮助。

基于@GWW答案

function getLocationArray($start){
    $link=dbConnect();//a function returning the link with your db
    $stack = array();
    $parent = $start;
    while($parent != 0){
        $query='SELECT catName,parentID from myTable where catId='.$parent;
        $result = mysql_query($query,$link);
        while($row = mysql_fetch_assoc($result)){
            $parent=$row['parentID'];
            $name=$row['catName'];
            $stack[] = $name;
            /*foreach($row as $cname => $cvalue){
            }*/
        }
    }
    $stack = array_reverse($stack);
    return $stack;
}
var_dump($stack);

阅读后,我仍然不确定如何获取芝加哥的parentID/父catname:(
-- TABLES

drop table if exists categories;
create table categories
(
cat_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
parent_cat_id smallint unsigned null,
key (parent_cat_id)
)
engine = innodb;

-- TEST DATA

insert into categories (name, parent_cat_id) values
('Location',null), 
('Color',null), 
   ('USA',1), 
      ('Illinois',3), 
      ('Chicago',3), 
   ('Black',2), 
   ('Red',2);


-- STORED PROCEDURES

drop procedure if exists category_parent;

delimiter #

create procedure category_parent
(
in p_cat_id smallint unsigned
)
begin

declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;

create temporary table hier(
 parent_cat_id smallint unsigned, 
 cat_id smallint unsigned, 
 depth smallint unsigned default 0
)engine = memory;

insert into hier select parent_cat_id, cat_id, v_depth from categories where cat_id = p_cat_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while not v_done do

    if exists( select 1 from categories p inner join hier on p.cat_id = hier.parent_cat_id and hier.depth = v_depth) then

        insert into hier 
            select p.parent_cat_id, p.cat_id, v_depth + 1 from categories p 
            inner join tmp on p.cat_id = tmp.parent_cat_id and tmp.depth = v_depth;

        set v_depth = v_depth + 1;          

        truncate table tmp;
        insert into tmp select * from hier where depth = v_depth;

    else
        set v_done = 1;
    end if;

end while;

select 
 c.cat_id,
 c.name as category_name
from 
 hier
inner join categories c on hier.cat_id = c.cat_id
where
 hier.parent_cat_id is null;


drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;


drop procedure if exists category_hier;

delimiter #

create procedure category_hier
(
in p_cat_id smallint unsigned
)
begin

declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;

create temporary table hier(
 parent_cat_id smallint unsigned, 
 cat_id smallint unsigned, 
 depth smallint unsigned default 0
)engine = memory;

insert into hier select parent_cat_id, cat_id, v_depth from categories where cat_id = p_cat_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while not v_done do

    if exists( select 1 from categories p inner join hier on p.parent_cat_id = hier.cat_id and hier.depth = v_depth) then

        insert into hier 
            select p.parent_cat_id, p.cat_id, v_depth + 1 from categories p 
            inner join tmp on p.parent_cat_id = tmp.cat_id and tmp.depth = v_depth;

        set v_depth = v_depth + 1;          

        truncate table tmp;
        insert into tmp select * from hier where depth = v_depth;

    else
        set v_done = 1;
    end if;

end while;

select 
 p.cat_id,
 p.name as category_name,
 b.cat_id as parent_cat_id,
 b.name as parent_category_name,
 hier.depth
from 
 hier
inner join categories p on hier.cat_id = p.cat_id
left outer join categories b on hier.parent_cat_id = b.cat_id
order by
 hier.depth, hier.cat_id;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;

-- TESTING (call this stored procedure from php)

call category_hier(1);

call category_hier(2);

call category_parent(5);

call category_parent(7);
function getLocationArray($start){
    $link=dbConnect();//a function returning the link with your db
    $stack = array();
    $parent = $start;
    while($parent != 0){
        $query='SELECT catName,parentID from myTable where catId='.$parent;
        $result = mysql_query($query,$link);
        while($row = mysql_fetch_assoc($result)){
            $parent=$row['parentID'];
            $name=$row['catName'];
            $stack[] = $name;
            /*foreach($row as $cname => $cvalue){
            }*/
        }
    }
    $stack = array_reverse($stack);
    return $stack;
}
var_dump($stack);