所以我正在编写这个程序,它本质上是一个星号的后端系统,它向管理显示所有呼叫,呼叫时间,呼叫日期,用户名等。以前的方式是你有一个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循环向数据库发出大量请求,因为我需要的数据是相同的,只是针对不同的分机号。
答案 0 :(得分: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库,它正在逐步淘汰。
答案 1 :(得分: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.
}
?>