2011-07-12 42 views
0

我有php脚本发送的值给MSSQL数据库。它大约有70%的时间工作(应该有〜100条记录,我会看到〜70)。我没有看到任何模式。我不想通过我已有的日志寻求帮助;我需要知道我应该在哪里寻找错误。这个php很简单,使用mssql_query,mssql_num_rows和mssql_fetch_array。在php.ini中的超时没有从默认的60s改变,虽然我现在刚刚设置为240s(如果这可以修复所有内容,我会更新该帖子)。脚本被调用后,我无法返回任何内容;每一个回应,无论是否有错误,都必须是相同的。我可以记录mssql_query函数的详细信息吗?或者是否有来自我可以听的SQL服务器的响应?我已阅读手册,并没有看到这样的事情。任何帮助,将不胜感激!我需要尽快照顾。

编辑 - 我不能运行mssql_showlastmessage,因为我不能返回任何东西,但事情会中断。以下是整个代码。PHP/MSSQL连接故障排除

<?php 
//request xml cdr from switchvox 
error_reporting(E_ALL); 

//include switchvox libraries 
require_once("SwitchvoxRequest.php"); 

//define sql connection stuff 
$db_host = "dbhost"; 
$db_user = "dbuser"; 
$db_pass = "dbsecret"; 
$db = "db"; 
$table_sr = "tblsr"; 
$table_cd = "tblcmrd"; 
$link = mssql_connect($db_host, $db_user, $db_pass); 
$err = ""; 

//make sure we can connect 
if (!$link || !mssql_select_db($db, $link)) 
     { 
      die("<response></response>"); 
     } 

//define pbx connection stuff 
$sv_host = "pbx"; 
$sv_user = "user"; 
$sv_pass = "secret"; 

//get the extension from the pbx and format it 
$ext = $_GET['ext']; 
$ext2 = $_GET['ext2']; 
$jobid = $_GET['jobid']; 
$et = $_GET['et']; 

if(!$ext2) 
     { 
     $ext = substr($ext,-4,4); 
     } 

if(strlen($ext) > 4) 
     { 
     if(strlen($ext2) > 4) 
       { 
       die("<response></response>"); 
       } 
     $ext = $ext2; 
     } 

//query the sr table to find the account ID of the extension we are referencing 
$acid_sql = "SELECT * FROM $table_sr WHERE ext=$ext"; 
$acid_res = mssql_query($acid_sql); 
$acida = mssql_fetch_array($acid_res, MSSQL_BOTH); 
$acid = $acida['pbx_accountid']; 

if (!$acid_res) 
     { 
      die("<response></response>"); 
     } 

//make sure there is a salesrep for the extension making the call 
if (!$acid) 
     { 
      die("<response></response>"); 
     } 

//get and format the time and date as YYYY-MM-DD 
$date = date('Y') . "-" . date('m') . "-" . date('d'); 

//format the time as HH:MM:SS 
$time = date('H') . ":" . date('i') . ":" . date('s'); 

//create a new request 
$req = new SwitchvoxRequest($sv_host, $sv_user, $sv_pass); 
$reqpar = array 
     (
     'account_ids' => array 
       (
       'account_id' => array 
         (
         $acid 
         ) 
       ), 
      'start_date' => array 
       (
       $date . " " . "00:00:00" 
       ), 
     'end_date' => array 
       (
       $date . " " . $time 
       ), 
     'sort_field' => array 
       (
       ), 
     'sort_order' => array 
       (
       'DESC' 
       ) 
     ); 
$res = $req -> send("switchvox.callLogs.search", $reqpar); 
$result = $res->getResult(); 
$calls = $result['calls']['total_items']; 

//check that there were calls to/from this account today 
if(!$calls) 
     { 
     die("<response></response>"); 
     } 

$latest = $result['calls']['call']['0']; 
$callid = $latest['id']; 

//check to see if the call has already been logged 
$id_sql = "SELECT * FROM $table_cd WHERE callID='$callid'"; 
$id_res = mssql_query($id_sql); 
$exid = mssql_fetch_array($id_res, MSSQL_ASSOC); 

if (!$id_res) 
     { 
    die("<response></response>"); 
     } 

if($exid['callID']) 
     { 
     die("<response></response>"); 
     } 

//define variables to be sent to the table 
$from = $latest['from_number']; 
$to = $latest['to_number']; 
$durat = $latest['talk_duration']; 
$start = $latest['start_time']; 
$callid = $latest['id']; 
$calltype = $latest['origination']; 

//check the length of the cid/ext strings and swap if needed 
if (strlen($from) > strlen($to)) 
     { 
     $extension = $to; 
     $phonenumber = $from; 
     } 
     else 
     { 
     $extension = $from; 
     $phonenumber = $to; 
     } 

//insert the data into the table 
$fi_sql = "INSERT INTO $table_cd (extension, phonenumber, calldatetime, duration, callID, calltype)  VALUES ($extension, $phonenumber, '$start', '$durat', '$callid', '$calltype')"; 
$fi_res = mssql_query($fi_sql); 

if (!$fi_res) 
{ 
    die("<response></response>"); 
} 

$sv_res = "<response></response>"; 

echo $sv_res; 
?> 

我意识到这是开放的sql注入。没关系。

编辑 -

我查看了数据库上的日志;当我期望插入数据时没有错误,甚至没有任何错误表明它尝试过。我将着眼于将日志消息记录到syslog或者来自php的东西。欢迎提出建议。

+0

你的错误日志说什么? – AndrewR

+0

在每次数据库调用后尝试mssql_get_last_message()以查看是否有用? – Naltharial

+0

'php很简单,使用mssql_query,mssql_num_rows和mssql_fetch_array.',然后向我们显示代码... –

回答

0

在一般情况下,你可以自由地执行这个MySQL的

EXPLAIN SELECT ... 

得到一个SELECT语句的描述。这个功能基本上解释了MySQL的查询分析器如何看待你的查询以及它将如何执行。例如,EXPLAIN描述了如果MySQL即将执行昂贵的表扫描或可能使用索引,并且如果是的话,哪一个。

你确定MySQL导致了这个问题吗?或者可能会PHP迫使问题?

您是否在每次查询后检查mysql_error()

$ ext可能不是数字吗?在这种情况下,$分机应该得到引用(对不起,不喜欢串的内部变量替换):

$acid_sql = "SELECT * FROM " . $table_sr . " WHERE ext='" . $ext . "'"; 

既然你返回XML,如果该程序的客户端表现良好,为什么不插入一个新元素响应这样的内部:

<response> 
    ... other elements ... 
    <sysInfo> 
    Failed to perform... 
    </sysInfo> 
</response> 

良好的运行得XML客户端应只读已知的元件,因此跳过附加元件。添加这样的逻辑返回码总是有用的。

PS:虽然上面的代码可能只是一个样本,让我一句话,说这是的SQL注射可攻击。

+0

我不认为我正在运行的任何查询都将数据库纳税到无法继续发送结果的程度......我误解了吗? – lorsungcu

+0

解释说明这些查询的成本如何。 MySQL将始终返回结果。如果你有问题,你应该检查MySQL的slowQuery计数器。 – SteAp

+0

我正在使用MS sql。在检查了sql服务器日志之后,在插入数据的时间段内没有任何错误或任何错误。 – lorsungcu