php
  • mysql
  • 2012-08-29 32 views 0 likes 
    0

    因此,我編寫的這個程序,本質上是一個後端系統星號,它顯示所有呼叫,通話時間,通話日期,用戶名等管理。之前的方式是,您有一個for循環遍歷數據庫中的所有用戶。裏面,這是一個多項功能,並始終用來調用此語句中的每個函數:過濾Mysql查詢後,它已被存儲在PHP變量

    $stmt="select callcharge,billsec,src,dst,calldate from cdr where (src='$ext[$x]' or src='238800$ext[$x]') and calldate>='$startdate' and calldate<='$stopdate' and length(dst)>3 and (dst like '7%' or dst like '9%') and billsec>5"; 
        $result=mysql_query ($stmt); 
    

    ,它將爲所有用戶做到這一點(每一個都有自己獨特的擴展){非常非常SLOWW!}

    我現在想的,而不是減少加載時間,調用此聲明,一旦外部for循環:

    $stmtf="select callcharge,billsec,src,dst,calldate from cdr where calldate>='$startdate' and calldate<='$stopdate' and length(dst)>3 and dst like '2%' and billsec>5"; 
    

    $結果=請求mysql_query($ stmtf);

    通知,它不是由分機號碼過濾了(它要求所有的每一個分機號碼的信息)

    現在我想做的事是這樣的:調用此MySQL查詢只有一次後,現在所有的數據存儲在變量$ stmtf中,我希望能夠進入for循環,並以某種方式過濾$ stmtf變量與此查詢(src='$ext[$x]' or src='238800$ext[$x]')

    我的目標是向數據庫發出一次請求,而不是因爲我需要的數據是相同的,只是針對不同的分機號碼,所以需要從for循環到數據庫。

    +0

    提供一個的print_r()的陣列結構。 –

    +0

    返回$ result的print_r():'Resource id#10' – iddrew

    +0

    好吧,這是您的第一個問題。您需要遍歷結果並將每行添加到數組中。這基本上會給你一個由數組組成的數組。 –

    回答

    0

    下面是一些代碼來說明我的評論:

    <?php 
    /** 
    * STORING THE RECORDS SORTED BY EXTENSION 
    */ 
    $stmtf="select callcharge,billsec,src,dst,calldate from cdr where calldate>='{$startdate}' and calldate<='{$stopdate}' and length(dst)>3 and dst like '2%' and billsec>5"; 
    $results=mysql_query ($stmtf); 
    $data = array(); 
    while($row = mysql_fetch_assoc($results)) { // cycle each result returned by the query. 
        $row['src'] = substr_replace("238800","",$row['src']); //normalize the 'src' to one format. 
        $data[$row['src']][] = $row; //store the record in a sub-array, sorted by 'src' as the first key. 
    } 
    print_r($data); //echo the sorted results for debugging. 
    
    /** 
    * CALLING THE ROWS FOR EXTENSION '98' 
    */ 
    
    foreach($data['98'] as $record) { //if all extensions are numeric, $data[98] will also work. 
        print_r($record); //each record contained in the sub-array for '98' is printed individually. 
    } 
    ?> 
    
    +0

    哦,這很有道理!我只是試了一下,它就像一個魅力!另外,加載時間要快得多!謝謝@Remco Overdijk! – iddrew

    0

    您可以通過查詢結果需要循環,是這樣的:

    $stmtf="select callcharge,billsec,src,dst,calldate from cdr where calldate>='$startdate' and calldate<='$stopdate' and length(dst)>3 and dst like '2%' and billsec>5"; 
    
    $results=mysql_query ($stmtf); 
    
    while($row=mysql_fetch_assoc($results)){ 
    //access vars like this - $row['callcharge'] 
    } 
    

    http://uk3.php.net/mysql_fetch_assoc

    哦,你可能要考慮遏制使用MySQL庫,它是被淘汰。

    +0

    輝煌!所以一旦我這樣做,我可以用特定的分機號碼拉行嗎? – iddrew

    +0

    你只能把檢查只在某些行上執行操作,例如如果($ row ['whatever'] =='xyz'){//做某事} –

    相關問題