dongzhui2636 2015-04-17 12:58
浏览 78
已采纳

PHP的SQLSRV间歇性地失败

I have a system running on Apache with PHP 5.5 and a database SQLSERVER 2012 Express.

After installed the extension "php_sqlsrv_55_ts" and add the extension in php.ini, i install the ODBC controller for my SO (Windows 8.1).

Well, im pretty sure i installed everything right.

When i run a php script, it returns the output var O.K (example in the end). But after a time, this script "fail" and dont return anything. Not error message, not var output, anything. This happens intermittently. Sometimes is work O.K, sometimes fail.

As you can see, i set a default var $outputVar to -1.

After the script execute the Procedure, i expected this overwrite my $outputVar, and this work O.K. The problem is, sometimes the script run without errors, but dont return anything, in consequence the $outputVar never is overwrite and the default value still is -1.

This happened with all my procedures, even with the most simplest.

This happens randomdly.

My PHP script.

$serverName = "xx.xx.xx.xx\SQLEXPRESS, 1433";

$connectionInfo = array(
    "Database"     => "xxxxx",
    "UID"          => "xxxxx",
    "PWD"          => "xxxxx",
    'CharacterSet' => 'UTF-8',
);

$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "ok.";
} else {
    echo "error";
}

$outputVar  = 'ANDRONICO'; // this is default value.

$parameters = array(
    array($outputVar, SQLSRV_PARAM_OUT),
);

$query = '{CALL TEST_SP(?)}';

$stmt = sqlsrv_query($conn, $query, $parameters);

sqlsrv_next_result($stmt);

if ($stmt === false) {
    $errors = sqlsrv_errors();
    var_dump($errors);
}

var_dump($outputVar); // this var is overwrite by SQLSRV library.

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

My Stored procedure return a random number from 1 to 1000

CREATE PROCEDURE [dbo].[test_1]
(
 @ret_cod int OUTPUT
) 
As 
BEGIN TRY

SET NOCOUNT On
SET @ret_cod = (SELECT ROUND(((999) * RAND() + 1), 0));


END TRY
BEGIN CATCH
    SET @ret_cod = (SELECT ERROR_NUMBER() AS ErrorNumber)
END CATCH
  • 写回答

1条回答 默认 最新

  • doutun1362 2015-04-21 13:45
    关注

    As said sofl,

    The problem was fixed adding the option to my procedures "SET NOCOUNT ON" and be careful with the procedure dont output nothing except the result i need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题