0

だから私はこのプログラムをコーディングしています。これは基本的にアスタリスクのバックエンドシステムであり、すべての通話、通話時間、通話日、ユーザー名などを管理者に表示します。以前は、データベース内のすべてのユーザーをループする for ループがありました。その中にはいくつかの関数があり、各関数は常にこのステートメントを呼び出すために使用されていました。

 $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);

すべてのユーザーに対してこれを行います (それぞれ独自の拡張子を持つ) {VERY VERY SLOWW!}

代わりに、このステートメントを for ループの外で 1 回呼び出すことで、読み込み時間を短縮しようとしています。

$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);

内線番号によるフィルタリングが行われていないことに注意してください (内線番号ごとにすべての情報を呼び出します)。

今私がやりたいことはこれです: この mysql クエリを一度だけ呼び出した後、すべてのデータが変数 $stmtf に格納された後、for ループに入り、このクエリでその $stmtf 変数を何らかの形でフィルタリングできるようにしたい(src='$ext[$x]' or src='238800$ext[$x]')

ここでの私の目標は、必要なデータは同じで、内線番号が異なるだけなので、for ループからデータベースに大量の要求を行うのではなく、データベースに一度要求を行うことです。

4

2 に答える 2

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.
}
?>
于 2012-08-29T13:00:28.857 に答える
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 ライブラリの使用を抑えることを考えた方がいいかもしれません。それは段階的に廃止されています。

于 2012-08-29T12:55:57.240 に答える