mysql分类表
id pid level name
1 0 0 test1
2 0 0 test2
3 1 1 test1.1
4 3 2 test1.1.1
php查询方法
mysql数据查询类忽略----(大家都懂的)
我的查询方法:
$my_rs = $my_conn->mysql_query('select * from table where pid = 0');
$class_rs = array();
$do_while_static=mysql_num_rows($my_rs)?true:false;
if($do_while_static) $prs=array($my_rs);
while($do_while_static){
$my_rs=end($prs);
if($rs=mysql_fetch_array($my_rs)){
$class_rs[]=$rs;
$my_prs = $my_conn->mysql_query('select * from table where pid = '.$rs['id']);
$temp_static=mysql_num_rows($my_prs)?true:false;
if($temp_static) $prs[]=$my_prs;
}else{
array_pop($prs);
if(count($prs)==0){
$do_while_static=false;
break;
}
}
}
foreach($class_rs as $rs){
$t_str=str_pad('',$rs['level'],-);
echo $t_str.$rs['name'];
}
以上就是我的查询和输出代码,我不知这个查询是不是算高效查询,所以我分享出来,让大家给点意见,或者大家分享一下自己的查询和输出方法,让我学习一下,谢谢大家了
mysql php
------解决方案--------------------
一般来讲没啥很大的问题的。
------解决方案--------------------
除非寫的非常離譜,要不然性能影響不大
------解决方案--------------------
$prs[]=$my_prs;
利用堆栈将递归化为循环,代码反而变得隐涩了
由于循环体内的代码量增加,效率应该不比递归强
