【问题标题】:looping php mysql query till no result found [duplicate]循环php mysql查询直到找不到结果[重复]
【发布时间】:2013-12-20 20:54:21
【问题描述】:

我有这个 mysql 查询,我想循环直到在表中找不到更多结果,

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

}

我想要的是像这样一次又一次地循环相同的查询:

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

}
}

这最后的代码将循环查询两次,但我遇到的问题是我想循环它直到找不到更多结果

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

}

循环 3 次:

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

$sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
 $res = mysql_query($sql_query);
 $ids=array();
 while($row = mysql_fetch_object($res)){
 $ids[]=$row->id;
}
 $ids=array_filter($ids);
 foreach($ids as $id){
 echo $id;

}
}
}

那么我如何无限次重复代码直到数据库中没有结果

【问题讨论】:

  • 你想要'递归'和'树结构'
  • 不,我认为他只想获取该表上的所有行,而代码只做了一次......
  • 是的,杰西卡,这就是我想要的,无论如何,例如如何做对?
  • 所以听起来你正试图在你的数据库表中表示一个树结构。您可能需要多考虑一下您的架构。您不需要递归查询表来获取此数据,而是如果您有一个设计正确的表架构,则能够在单个查询中获取数据。有关更多信息,请参阅这些链接:mikehillyer.com/articles/managing-hierarchical-data-in-mysqlstackoverflow.com/questions/5916482/…

标签: php mysql sql


【解决方案1】:

使用递归的快速解决方案:

function getChildIds($id, &$count) { // Note that $count is passed in as a reference (the & before the $count)
    ++$count;
    echo $id;
    $sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
    $res = mysql_query($sql_query);
    $ids = Array();
    while($row = mysql_fetch_object($res)){
        if ($row->id) { // Checking for false-like values here to remove the need for array_filter
            $ids[] = $row->id;
        }
    }

    foreach ($ids as $next) { // using foreach instead of array_walk as we cannot pass a reference into array_walk without raising a warning (or fatal error, depending on PHP version)
        getChildIds($next, $count);
    }
}

$count = 0;
getChildIds($firstId, $count);
// $count now has the number of times the function was called
echo $count;

但请注意,mysql_* functions are deprecated 而您应该改用mysqliPDO

【讨论】:

  • 你没有回显id!
  • 回显 $id;是函数的第一行。
  • 哦,对不起,错过了...但它应该在 if... 因为至少有一个不会有数据!无论如何你都会打印它吗?!
  • 有没有办法计算回显的 id?
  • @JorgeCampos 不,if ($row->id) 检查确保在检索到的 $id 为空(或零)时不会调用该函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 2012-07-30
  • 2014-01-08
  • 2013-05-13
  • 2015-11-10
  • 2016-10-29
相关资源
最近更新 更多