php查询数据遍历显示 php遍历数据库( 二 )


?php
$arr=
array();
for($i=
0;
$i
50000;
$i++){
$arr[]=
$i*rand(1000,9999);
}
function
GetRunTime()
{
list($usec,$sec)=explode("
",microtime());
return
((float)$usec+(float)$sec);
}
######################################
$time_start=
GetRunTime();
for($i=
0;
$i
count($arr);
$i++){
$str=
$arr[$i];
}
$time_end=
GetRunTime();
$time_used=
$time_end-
$time_start;
echo
'Used
time
of
for:'.round($time_used,
7).'(s)br
/br
/';
unset($str,
$time_start,
$time_end,
$time_used);
######################################
$time_start=
GetRunTime();
while(list($key,
$val)=
each($arr)){
【php查询数据遍历显示 php遍历数据库】$str=
$val;
}
$time_end=
GetRunTime();
$time_used=
$time_end-
$time_start;
echo
'Used
time
of
while:'.round($time_used,
7).'(s)br
/br
/';
unset($str,
$key,
$val,
$time_start,
$time_end,
$time_used);
######################################
$time_start=
GetRunTime();
foreach($arr
as$key=
$val){
$str=
$val;
}
$time_end=
GetRunTime();
$time_used=
$time_end-
$time_start;
echo
'Used
time
of
foreach:'.round($time_used,
7).'(s)br
/br
/';
?
测试结果:
Used
time
of
for:0.0228429(s)
Used
time
of
while:0.0544658(s)
Used
time
of
foreach:0.0085628(s)
经过反复多次测试,结果表明,对于遍历同样一个数组,foreach速度最快,最慢的则是while 。从原理上来看,foreach是对数组副本进行操作(通过拷贝数组),而while则通过移动数组内部指标进行操作,一般逻辑下认为,while应该比foreach快(因为foreach在开始执行的时候首先把数组复制进去,而while直接移动内部指标 。) , 但结果刚刚相反 。原因应该是,foreach是PHP内部实现 , 而while是通用的循环结构 。所以,在通常应用中foreach简单,而且效率高 。在PHP5下,foreach还可以遍历类的属性 。
以上所述就是本文的全部内容了,希望大家能够喜欢 。
在php中有张数据表 如果一个字段的值分多级分类,怎么才能遍历查询出?分开显示到模板?/**
*如果出现多级分类且在一张表中, 此时你需要增添字段确定分类间php查询数据遍历显示的关系
*通常做法是在数据表中加一个字段如 'pid', 如果 id == pid 即判定为父子关系
*/
// 如这张表
// 如果需要找出中国下面的分类php查询数据遍历显示,只需要找出所有 pid = 1 即可
SELECT * FROM table WHERE pid = 1;
php通过mysql like搜索关键词查询 , 所有符合数据遍历出来?php
$host="localhost";
$username="root";
$password="root";
$db="db4";//库名
$mysql_table="person";//表名
//连接数据库,面向过程
$conn=mysqli_connect($host,$username,$password);
if(!$conn){
echo "数据库连接失败";
exit;
}
//选择所要操作php查询数据遍历显示的数据库
mysqli_select_db($conn,$db);
//设置数据库编码格式
mysqli_query($conn,"SET NAMES UTF8");
//编写sql获取分页数据 SELECT * FROM 表名 LIMIT 起始位置php查询数据遍历显示,显示条数
//注意:以下id,name,age,say都是字段节点名,person是表名,db4是数据库名,think是指定php查询数据遍历显示的关键字.

推荐阅读