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

它会为所有用户执行此操作(每个用户都有自己独特的扩展名){非常非常慢!}

现在我试图减少加载时间,而是在 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";

$results=mysql_query ($stmtf);

请注意,它不再按分机号码过滤(它调用每个分机号码的所有信息)

现在我想做的是:在调用这个mysql查询一次之后,现在所有数据都存储在变量$stmtf中,我希望能够进入for循环并以某种方式过滤这个查询$stmtf变量(src='$ext[$x]' or src='238800$ext[$x]')

我的目标是向数据库发出一次请求,而不是从 for 循环向数据库发出大量请求,因为我需要的数据是相同的,只是用于不同的分机号。

4

2 回答 2

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 回答
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 回答