2011-07-12 97 views
0

私は与えられた値をMSSQLデータベースに送信しているPHPスクリプトを持っています。それは時間の約70%を働かせます(〜100のレコードがあるはずです、私は〜70を見るでしょう)。私が見ることができるパターンはありません。私は既に持っているログを調べるのに助けを求めていません。私はエラーを見つけるためにどこを見なければならないかを知る必要があります。 PHPは、mssql_query、mssql_num_rows、およびmssql_fetch_arrayを使用して簡単です。 php.iniのタイムアウトはデフォルトの60から変更されていませんが、今は240に設定しています(これですべてが修正されれば、私はポストを更新します)。スクリプトが呼び出されたら何も返せません。エラーがあるかどうかにかかわらず、すべての応答は同じでなければなりません。どういうわけかmssql_query関数の詳細をログに記録できますか?または、私が聞くことができるSQLサーバーからの応答がありますか?私はマニュアルを読んだが、このようなことは何も見なかった。どんな助けもありがとう!私はこれをできるだけ早く世話する必要があります。

edit - 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

DBの呼び出しのたびにmssql_get_last_message()を実行して、役に立つかどうか確認してください。 – Naltharial

+0

'phpはmssql_query、mssql_num_rows、およびmssql_fetch_arrayを使用して簡単です.'その後、コードを表示します... –

答えて

0

、あなたは、SELECT文のの説明を取得するためのMySQL

EXPLAIN SELECT ... 

でこれを実行するのは自由です。この機能は、MySQLのクエリアナライザがクエリをどのように見て、どのように実行されるかを基本的に説明します。たとえば、EXPLAINは、MySQLが高価なテーブルスキャンを実行しようとしている場合、またはインデックスを使用している場合はインデックスを使用するかどうかを示します。

本当に、MySQLが問題の原因ですか?またはPHPの力の問題がありますか?

クエリごとにmysql_error()をチェックしますか?

おそらく$ extは数字ではありませんか?この場合、$ 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 Serverログをチェックしたところ、データが挿入されなかった期間にエラーや何もありません。 – lorsungcu