2011-07-12 40 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