select多次,跟select一次。然后foreach对比,后者效率高吧?下面的代码怎么改?不会php。高分送上。<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/$timep = date( "YmdHis" );
$fc = "rate/common.dat";
$filestr = 1;
$tdu = 0;
$sqlau = mysql_query( "select auto_num,auto_low,low_cut from auto_cut where sign='1'", $conn );
$num_limit = mysql_result( $sqlau, 0, "auto_num" );
$once_rate = mysql_result( $sqlau, 0, "auto_low" );
$low_rate = mysql_result( $sqlau, 0, "low_cut" );
$i = 1;
for ( ; $i <= 49; ++$i )
{
$rates = 0;
if ( strlen( $i ) == 1 )
{
$n = "0".$i;
}
else
{
$n = $i;
}
$sqlf = mysql_query( "select sum(drop_money) from drop_bill where drop_date='{$dates}' and drop_content='{$n}' and cut_sign='1'", $conn );
$mys = mysql_fetch_array( $sqlf );
$mycount = $mys[0];
if ( $mycount == "" )
{
$mycount = 0;
}
$sqlt = mysql_query( "select bnum,rate from num where num='{$n}' and cut_sign='1' and user_account='admin'", $conn );
$bnum = mysql_result( $sqlt, 0, "bnum" );
$rate = mysql_result( $sqlt, 0, "rate" );
$cnum = intval( $mycount / $num_limit ) - $bnum;
$nbnum = intval( $mycount / $num_limit );
if ( 1 <= $cnum )
{
$rates = $rate - $cnum * $once_rate;
if ( $low_rate <= $rates )
{
$sqlg = mysql_query( "select rate from num where num='{$n}' and cut_sign='1' and user_account='admin'", $conn );
if ( mysql_num_rows( $sqlg ) != 0 )
{
$gup = mysql_result( $sqlg, 0, "rate" );
mysql_query( "update num set brate='{$gup}' where num='{$n}' and cut_sign='1'", $conn );
}
mysql_query( "update num set rate='{$rates}',m='1',h='1',bnum='{$nbnum}',uptime='{$timep}' where num='{$n}' and cut_sign='1'", $conn );
$tdu = 1;
}
}
}
$i = 1;
for ( ; $i <= 49; ++$i )
{
$rates = 0;
if ( strlen( $i ) == 1 )
{
$n = "0".$i;
}
else
{
$n = $i;
}
$sqlf = mysql_query( "select sum(drop_money) from drop_bill where drop_date='{$dates}' and drop_content='{$n}' and cut_sign='0'", $conn );
$mys = mysql_fetch_array( $sqlf );
$mycount = $mys[0];
if ( $mycount == "" )
{
$mycount = 0;
}
$sqlt = mysql_query( "select bnum,rate from num where num='{$n}' and cut_sign='1' and user_account='admin'", $conn );
$bnum = mysql_result( $sqlt, 0, "bnum" );
$rate = mysql_result( $sqlt, 0, "rate" );
$cnum = intval( $mycount / $num_limit ) - $bnum;
$nbnum = intval( $mycount / $num_limit );
if ( 1 <= $cnum )
{
$rates = $rate - $cnum * $once_rate;
if ( $low_rate <= $rates )
{
$sqlg = mysql_query( "select rate from num where num='{$n}' and cut_sign='1' and user_account='admin'", $conn );
if ( mysql_num_rows( $sqlg ) != 0 )
{
$gup = mysql_result( $sqlg, 0, "rate" );
mysql_query( "update num set brate='{$gup}' where num='{$n}' and cut_sign='1'", $conn );
}
mysql_query( "update num set rate='{$rates}',m='1',h='1',bnum='{$nbnum}',uptime='{$timep}' where num='{$n}' and cut_sign='1'", $conn );
$tdu = 1;
}
}
}
$tdsa = array( "特单", "特双", "特大", "特小", "合单", "合双", "红波", "蓝波", "绿波", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪", "三肖", "四肖", "五肖", "六肖" );
$cutsign = array( "2", "2", "3", "3", "4", "4", "8", "8", "8", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "9", "15", "15", "15", "15" );
$autos = array( "2", "2", "4", "4", "6", "6", "8", "8", "8", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "11", "23", "23", "23", "23" );
$i = 0;
for ( ; $i < count( $tdsa ); ++$i )
{
$sqlau = mysql_query( "select auto_num,auto_low,low_cut from auto_cut where sign='{$autos[$i]}'", $conn );
$num_limit = mysql_result( $sqlau, 0, "auto_num" );
$once_rate = mysql_result( $sqlau, 0, "auto_low" );
$low_rate = mysql_result( $sqlau, 0, "low_cut" );
$sqlf = mysql_query( "select sum(drop_money) from drop_bill where drop_date='{$dates}' and drop_content='{$tdsa[$i]}' and cut_sign='{$cutsign[$i]}'", $conn );
$mys = mysql_fetch_array( $sqlf );
$mycount = $mys[0];
if ( $mycount == "" )
{
$mycount = 0;
}
$sqlt = mysql_query( "select bnum,rate from num where tp='{$tdsa[$i]}' and cut_sign='{$cutsign[$i]}' and user_account='admin'", $conn );
$bnum = mysql_result( $sqlt, 0, "bnum" );
$rate = mysql_result( $sqlt, 0, "rate" );
$cnum = intval( $mycount / $num_limit ) - $bnum;
$nbnum = intval( $mycount / $num_limit );
if ( 1 <= $cnum )
{
$rates = $rate - $cnum * $once_rate;
if ( $low_rate <= $rates )
{
$sqlg = mysql_query( "select rate from num where tp='{$tdsa[$i]}' and cut_sign='{$cutsign[$i]}' and user_account='admin'", $conn );
if ( mysql_num_rows( $sqlg ) != 0 )
{
$gup = mysql_result( $sqlg, 0, "rate" );
mysql_query( "update num set brate='{$gup}' where tp='{$tdsa[$i]}' and cut_sign='{$cutsign[$i]}'", $conn );
}
mysql_query( "update num set rate='{$rates}',m='1',h='1',bnum='{$nbnum}',uptime='{$timep}' where tp='{$tdsa[$i]}' and cut_sign='{$cutsign[$i]}'", $conn );
$tdu = 1;
}
}
}
if ( $tdu == 1 )
{
fwrite( fopen( $fc, "w" ), $filestr, 5 );
mysql_query( "update member set update1='1'", $conn );
}
echo "\r\n\r\n";
?>

解决方案 »

  1.   

    如果select所有的出来,而后用php计算。是不是效果会好些?
      

  2.   

    select多次,跟select一次。然后foreach对比,后者效率高通常情况下是这样的.
    上面的代码看着太累,逻辑和数据处理混乱在了一起,这样的程序效率自然不会高的. 而且这样的代码可读性也非常差.
    建议:
    把PHP连接MYSQL的相关函数进行类封装,
    然后把对数据处理的部分进行函数封装,
    把逻辑整理一下OK了,
    而且这样也容易测试具体哪里效率低下,以便优化.
    这里副上一个简单的PHP相关MYSQL函数封装类.
    <?php
    /**
     * @createTime 2006-08-18
     * @author  ALFA(The Other Name is 'fafa211')
     * @package  mysql 
     * @todo  it is the mysql db class for study
    **/class DBQuery
    {  
        var $hostName = "localhost";
        var $hostPort = "3306"; 
        var $dataBase = "bbs";
        var $userName = "root";
        var $password = "";
        var $queryResult;
        
        /**
        * @createTime 2006-08-18
        * @author  ALFA
        * @param  null
        * @return  
        * @todo use new DBQuery(),construct function, use for connecting to the mysql server and selecting the database    
        **/
        function DBQuery()
        {
              mysql_connect($this->hostName.":".$this->hostPort, $this->userName, $this->password) or die("Connect to database error. Please connect to the manager");
              mysql_select_db($this->dataBase) or die("Connect to database error. Please connect to the manager");
        }
        /**
        * @createTime 2006-08-18
        * @author  ALFA
        * @param  string $sql
        * @return  array $result
        * @todo sent sql statement to mysql server 
        * and get the result from the server  
        **/
        function query($sql)
        {
            $result = mysql_query($sql);
            $this->queryResult = array();
            while ($rows = mysql_fetch_array($result, MYSQL_ASSOC)) 
            {
                $this->queryResult[] = $rows;
            }
            mysql_free_result($result);
            return $this->queryResult;
        }
        /**
        * @createTime 2006-08-18
        * @author  ALFA
        * @param  string $sql
        * @return  int or string $result
        * @todo sent sql statement to mysql server 
        * and get the result from the server ,the result type is int or string
        **/
        function getOne($sql)
        {
            $result = mysql_query($sql);
            while ($rows = mysql_fetch_array($result, MYSQL_NUM)) 
            {
                $this->queryResult = $rows[0];
            }
            mysql_free_result($result);
            return $this->queryResult;
        }
        /**
        * @createTime 2006-08-18
        * @author  ALFA
        * @param  string $sql
        * @return  bool
        * @todo sent sql statement to mysql server 
        * and get the result from the server ,the result is true or false
        **/
        function update($sql)
        {
            $this->queryResult = mysql_query($sql);
            return $this->queryResult;
        }}
    ?>