【问题标题】:db2_connect() returns resource ID despite failing尽管失败,db2_connect() 仍返回资源 ID
【发布时间】:2015-09-24 13:35:28
【问题描述】:

我正在使用 PHP 脚本中的 ibm_db2 驱动程序在 AS/400 V7R2 上运行。

我注意到,如果我使用i5_libl 选项将无效的库列表传递给db2_connect(),并且连接字符串的其余部分有效,尽管ini_set("display_errors", 1); 报告了错误,它仍将返回资源ID。此外,db2_conn_error() 和 db2_conn_errormsg() 不包含任何内容。另一方面,当我提供一个有效的库列表时,我的IF 语句的评估方式完全相同,唯一的区别是ini_set("display_errors", 1); 没有将错误输出到屏幕

我意识到,不是因为无效的库列表而失败,而是使用提供的 DB 用户名的默认库列表建立连接。这对我来说可能很可怕,因为如果由于某种原因我的库列表无效,它将默认为错误的列表(主要关注的是开发和生产环境的混合)。

其他人可以重现这种行为吗?我不知道这是否只是我的系统并且我需要 PTF,或者这是否是典型的。您如何验证已使用预期选项建立 DB2 连接?

要重现的代码(相应地替换系统名称、用户名和密码):

<?php
ini_set("display_errors", 1);
$systemName = 'yourSystemName';
$userID = 'yourUserID';
$password = 'yourPassword';
$options['i5_libl'] = implode('Z', array(
    'INVALID',        
    'LIB',       
    'LIST',   
    'IMPLODED',  
    'WITH',   
    'THE',      
    'LETTER',       
    'Z'
));
$options['i5_naming'] = DB2_I5_NAMING_ON;

$conn = db2_connect($systemName,$userID,$password,$options);
//The error output to the screen at this point from `ini_set("display_errors", 1);` is:
//Warning: db2_connect(): Statement Execute Failed in /PATH/TO/FILE/test.php on line 58 

echo "<br />|".db2_conn_error()." ||| ".db2_conn_errormsg()."|<br />"; //This displays as "| ||| |"
print_r($conn); //This prints out a resource ID
echo "<br />";

if(isset($conn) && $conn === true){ 
    //Expected to not pass since either false or a resource ID is supposed to be returned.
    //Evaluated to false.
    echo "Boolean true<br />";
}
if(isset($conn) && $conn == true){ 
    //Despite the connection failing according to `ini_set("display_errors", 1)` and a resource ID being reportedly returned
    //this evaluates to true and "Non-Boolean true 2" echos out to the screen.
    echo "Non-Boolean true 2<br />";  
}
if(isset($conn) && $conn == "true"){ 
    //Evaluates to false. db2_connect() returns a resource ID on success, so I did not expect this to evaluate to true.
    echo "String true";
}
if(isset($conn) && $conn === false){ 
    //Expected for this to evaluate to true since an error was logged by ini_set("display_errors", 1)
    //This did not evaluate to true.
    echo "Boolean false<br />";
}
if(isset($conn) && $conn == false){ 
    //Just checking to see if a non-Boolean false was returned.  Evaluates to false.
    echo "Non-Boolean false 2<br />";
}
if(isset($conn) && $conn == "false"){ 
    //Just checking to see if the string "false" was returned.  Evaluates to false.
    echo "String false";
}
?>

【问题讨论】:

    标签: php db2-400 db2-connect ibm-db2


    【解决方案1】:

    我最终用 Zend 开了一张票,结果证明这是故意的行为。

    我们知道i5_libl 选项根据http://php.net/manual/en/function.db2-connect.php 调用qsys2/qcmdexc('cmd',cmdlen)。但从逻辑上讲,必须建立连接才能知道要更改哪个 QSQSRVR 作业的库列表。它不会在失败时关闭连接,而是发出警告,db_conn_error() 和 db2_connerrormsg() 会正常报告一切。

    幸运的是,这记录为语句错误,可以通过以下方式检测到:

    if (db2_stmt_error()) {
        echo "error ID: " . (db2_stmt_error());
        echo "<br>error message: " . (db2_stmt_errormsg());
    }
    

    我建议 Zend 关闭连接或强制连接错误之类的。我想不出任何我想使用库列表连接到数据库的实例,而不是我指定的库列表,而没有任何警告或公然通知。如果开发和生产数据相互交织,这可能会导致灾难。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多