Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MSSQL连接疑难解答_Php_Apache_Logging - Fatal编程技术网

PHP/MSSQL连接疑难解答

PHP/MSSQL连接疑难解答,php,apache,logging,Php,Apache,Logging,我有一个php脚本,它将给定的值发送到MSSQL数据库。它大约70%的时间都能工作(应该有100条记录,我会看到70条)。我看不出有什么模式。我不是在寻找帮助,我已经有了日志;我需要知道应该在哪里查找错误。php很简单,使用mssql_查询、mssql_num_行和mssql_fetch_数组。php.ini中的Timeout没有从默认的60秒更改,尽管我刚刚将其设置为240秒(如果这修复了所有问题,我将更新帖子)。调用脚本后,我无法返回任何内容;无论是否有错误,每个响应都必须相同。我可以记录m

我有一个php脚本,它将给定的值发送到MSSQL数据库。它大约70%的时间都能工作(应该有100条记录,我会看到70条)。我看不出有什么模式。我不是在寻找帮助,我已经有了日志;我需要知道应该在哪里查找错误。php很简单,使用mssql_查询、mssql_num_行和mssql_fetch_数组。php.ini中的Timeout没有从默认的60秒更改,尽管我刚刚将其设置为240秒(如果这修复了所有问题,我将更新帖子)。调用脚本后,我无法返回任何内容;无论是否有错误,每个响应都必须相同。我可以记录mssql_查询函数的详细信息吗?或者我可以监听SQL server的响应吗?我读过说明书,没见过这样的东西。任何帮助都将不胜感激!我需要尽快解决这个问题。

编辑- 我无法运行mssql_showlastmessage,因为我无法返回任何内容,但会出现问题。下面是完整的代码

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

通常,您可以在MySQL上自由执行此操作

EXPLAIN SELECT ...
获取SELECT语句的描述。这个特性基本上解释了MySQL的查询分析器如何查看您的查询以及如何执行查询。例如,
EXPLAIN
描述了MySQL是否将要执行代价高昂的表扫描,或者是否可能使用索引,如果是,是哪一个

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

您是否在每次查询后进行检查

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

既然您返回XML,并且如果此例程的客户端运行良好,为什么不在响应中插入一个新元素,如下所示:

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

... 其他元素。。。
无法执行。。。
一个性能良好的XML客户机应该只读取已知元素,从而跳过其他元素。添加这样的逻辑返回代码总是有用的


PS:尽管上面的代码可能只是一个示例,但让我注意一下,它可以通过sql注入进行攻击

错误日志怎么说?在每次DB调用后尝试mssql_get_last_message(),看看是否得到有用的东西?
php很简单,使用mssql_查询,mssql_num_行,和mssql_fetch_数组。
然后向我们显示代码…您说:“我无法运行mssql_showlastmessage,因为我无法返回任何内容,但会出现问题。”问题已经解决,因此我看不出您有选择。你需要找出哪里出了问题,错误信息会告诉你。在这一点上,我不知道是因为我添加了一些不应该出现的东西,还是因为其他原因。使用“”以外的任何内容进行响应都会导致“未定义的行为”。我不认为我正在运行的任何查询都会使数据库负担过重,无法继续发送结果……我是否误解了?解释描述了这些查询的成本。MySQL将始终返回结果。如果您有问题,您可能应该检查MySQL slowQuery计数器。我使用的是MS sql。检查了sql server日志后,在未插入数据的期间没有错误或任何内容。$ext将始终是一个4位数字。好的,如果没有跟踪调试信息的选项,我看不到解决问题的简单方法。为什么不创建一个新表来记录此例程的消息?
<response>
   ... other elements ...
  <sysInfo>
    Failed to perform...
  </sysInfo>
</response>