当前位置: 首页 > 图文教程 > 网络编程 > PHP > 影响PHP+MYSQL执行速度的写法对比

PHP
php 多线程上下文中安全写文件实现代码
PHP类的使用 实例代码讲解
用php实现让页面只能被百度gogole蜘蛛访问的方法
php 学习笔记
PHP编程过程中需要了解的this,self,parent的区别
php 操作excel文件的方法小结
使用PHP获取网络文件的实现代码
PHP 巧用数组降低程序的时间复杂度
php下将XML转换为数组
php 文件上传代码(限制jpg文件)
php 无极分类(递归)实现代码
PHP 采集获取指定网址的内容
PHP 将图片按创建时间进行分类存储的实现代码
PHP 存储文本换行实现方法
PHP 批量更新网页内容实现代码
用PHP查询搜索引擎排名位置的代码
用php实现的获取网页中的图片并保存到本地的代码
php实现首页链接查询 友情链接检查的代码
处理php自动反斜杠的函数代码
php实现的遍历文件夹下所有文件,编辑删除

影响PHP+MYSQL执行速度的写法对比


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 458 ::
收藏到网摘: n/a

[code]<?php
include_once("../db.php");
if($_GET[x2]==0 and $_GET[x1]==0)
{
$t=" s1='' ";
}else
{
if($_GET[x2]!=0)
{
$t=" x2='$_GET[x2]' ";
}else
if($_GET[x1]!=0)
{
$t=" x1='$_GET[x1]' and x2=0 ";
}
}
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
$page_size=5;
if($amount%$page_size==0 and $amount>0)
{
$pagecount=($amount/$page_size);
}else
{
$pagecount=intval($amount/$page_size)+1;
}
if($_GET ==0)
{$_GET =1;}
if($_GET >$pagecount)
{$_GET =$pagecount;}
$page=$_GET ;
$a=($_GET -1)*$page_size;
$query = "select tid,truename,school,study,city,logo,year,month,x1,x2 from ask_member where $t order by logo desc, logintime desc limit $a,$page_size ";
$result = mysql_db_query($DataBase, $query);
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
$n=count($r2)-1;

?>




<?php
for($i=0;$i<$n;$i++)
{
?>

<table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#57ACF2">
<tr>
<td align="center" bgcolor="#FFFFFF">
<table width="690" border="0" cellspacing="0" cellpadding="0">
<tr valign="middle">
<td width="80"><img
src="../ask/center/memberlogo/<?php if($r2[$i][logo]!=''){echo $r2[$i][logo];}else if ($r2[$i][sex]=='女'){echo "/upload/tech/20091012/20091012013937_605ff764c617d3cd28dbbdd72be8f9a2.gif";}else{echo "/upload/tech/20091012/20091012013937_08419be897405321542838d77f855226.gif";} ?>" border=0 width="100" height="127" onload="javascript:DrawImage(this,100,127)"></td>
<td width="430"><strong><?php echo $r2[$i][truename]; ?> - <font color="#85B000">信息 :)</font></strong><br>
字段一:<a href="#" class="blue"><?php echo $r5[$i][name]==''?'内容':$r5[$i][name]; ?></a> <br>
字段二:<a href="#" class="blue"><?php echo $r2[$i][study]; ?></a><br>
字段三:<a href="#" class="blue"><?php echo $r2[$i][year]; ?><?php echo $r2[$i][month]; ?></a><br>
浏览:<a href="#" class="blue">一</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">二</a> <font color="#0E6FBE">|</font>
<a href="#" class="blue">三</a> </td>
<td width="180" align="right"><table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="hello.php?tid=<?php echo $r2[$i][tid]; ?>">打个招呼</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="sendmessagelook.php?tid=<?php echo $r2[$i][tid]; ?>">其它操作</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="20" align="center" bgcolor="#eeeeee"><a href="addfriend.php?tid=<?php echo $r2[$i][tid]; ?>">操作二</a></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3"></td>
</tr>
</table>

</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10"></td>
</tr>
</table>

<?php
}
?> [/code]
问题一:为什么要用

复制代码 代码如下:
<?php
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>

复制代码 代码如下:
<?php
while($r2[] = mysql_fetch_array($result))
{
}
foreach($r2 as $rr2)
{
$query = "select name,tid from ask_school where tid='$rr2[x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[] = mysql_fetch_array($result2);
}
?>


而不用:
复制代码 代码如下:
<?php
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>

复制代码 代码如下:
<?php
$j=0;
while($r2[$j] = mysql_fetch_array($result))
{
$query = "select name,tid from ask_school where tid='$r2[$j][x2]' ";
$result2 = mysql_db_query($DataBase, $query);
$r5[$j] = mysql_fetch_array($result2);
$j++;
}
?>


答:因为PHP网站在运行过程中,真正影响速度的原因只有一个,那就是对MYSQL的读取,至于程序多运行一两次复制和拷贝,很难对速度造成影响.所以在读取MYSQL的时候,我宁愿多一次内存中的运算,也不愿有什么操作影响到对MYSQL的读取.
问题二:为什么不用模版写程序?
答:因为PHP已经是最好的模版引擎了,为什么还要用其它的模版呢?

问题三:为什么把大量的PHP代码放在<HTML>的上方,而不是放面HTML的下方?
答:这个是有很大区别的,其一:代码放在<HTML>上方,如果对代码进行修改的话,用DREARWEAVER再看下面视图的时候,不会有慢的感觉.
其二:大部分代码全部放在<HTML>上方,而不是放在<HTML>下方,对PHP脚本的解析运行有好处,可以减轻PHP服务器解析脚本的负担,并且符合模版及缓存设计思想.
问题三:为什么要用
view plaincopy to clipboardprint?
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>
<?php
$query = "select count(*) from ask_member where $t ";
$result2 = mysql_db_query($DataBase, $query);
$r3 = mysql_fetch_array($result2);
$amount=$r3[0];
?>

而不是用:
view plaincopy to clipboardprint?
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>
<?php
$query = "select * from ask_member ";
$result2 = mysql_db_query($DataBase, $query);
$amount=mysql_num_rows($result2);
?>

答:第一种是对一条记录的查询,第二种是对所有记录的查询,两者速度有天壤之别.

问题四:为什么上面要多一行
view plaincopy to clipboardprint?
<?php
$n=count($r2)-1;
?>
<?php
$n=count($r2)-1;
?>

而不是下面直接用:
view plaincopy to clipboardprint?
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>
<?php
for($i=0;$i<count($r2)-1;$i++)
{
?>

答:应该尽量减少在<HTML>以下进行PHP的运算,如果过多地在<HTML>下进行运算的话,那就成了混编了.

问题五:为什么说以上代码是核心代码?
答:任何一位程序员都知道,读数据的分页读出是最常用的,我想大部分的PHP程序员30%的时间都是发在这个上面吧.
以上是我总结出的最快的写PHP的经验,请广大网友指正,如果觉得可以的话,希望所有人都能用这种方式来写程序,那样的话我以后安排一批PHP程序员进行分工协调的时候也好办多了.