sqlsrv_query()期望参数1为资源,布尔值为

时间:2017-02-23 08:55:25

标签: arrays fetch sqlsrv

我发现了一个旧项目,不知怎的,它不再起作用了?我的意思是,我们已经关闭了它,现在已经不再了。

    function fetchCharacter($UserNo){
        $connection_array = array( "Database"=>"Character", "UID"=>"sa", "PWD"=>"Password");
        $testcon = sqlsrv_connect("D-PC\SQLExpress", $connection_array);
        $sql = "SELECT * FROM tCharacter WHERE nUserNo = ? AND bDeleted = ?";
        $param = array($UserNo, 0);
        $characters = sqlsrv_query($testcon, $sql, $param);

        while($character = sqlsrv_fetch_array($characters, SQLSRV_FETCH_ASSOC))

所以我真的不知道那里有什么问题。我收到了sqlsrv_querysqlsrv_fetch_array的错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您应该在sqlsrv函数后查找错误。

function fetchCharacter($UserNo) {
    $connection_array = array( "Database"=>"Character", "UID"=>"sa", "PWD"=>"Password");

    $testcon = sqlsrv_connect("D-PC\SQLExpress", $connection_array);

    if (!$testcon) {
       die( print_r( sqlsrv_errors(), true));
    }

    $sql = "SELECT * FROM tCharacter WHERE nUserNo = ? AND bDeleted = ?";
    $param = array($UserNo, 0);
    $characters = sqlsrv_query($testcon, $sql, $param); // telling $stmt instead of $characters could be less confusing

    if( !$characters ) {  
       die( print_r( sqlsrv_errors(), true));
    }

    $character = sqlsrv_fetch_array($characters, SQLSRV_FETCH_ASSOC);

    if($character === FALSE) {
       die( print_r( sqlsrv_errors(), true));
    } else if ($character === NULL) {
       // no result from select
       return;
    }

    do {
       // some operations
    } while($character = sqlsrv_fetch_array($characters, SQLSRV_FETCH_ASSOC));

    ...

参考