当前位置: 代码网 > it编程>编程语言>Php > 影响PHP+MYSQL执行速度的写法对比

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

2024年05月18日 Php 我要评论
影响PHP+MYSQL执行速度的写法对比
-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 "default.gif";}else{echo "default3.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网站在运行过程中,真正影响速度的原因只有一个,那就是对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程序员进行分工协调的时候也好办多了.

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com