sqlsrv_num_rows does not work

3

I have a site that is being done with PHP + SQL SERVER, and to log in I need the client code, and it puts there only that always says it is incorrect, being correct.

It seems that sqlsrv_num_rows is not working, because I put the correct login and it does not enter the site that I did.

Login validation code:

    <?php 
session_start();

$serverName = "10.0.0.0.0";
$connectionInfo = array( "Database"=>"banco", "UID"=>"usuario", "PWD"=>"senha" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}


$var1 = $_POST['codMont']; 

$sql = "SELECT *
FROM AB7030 AB7,ABB030 ABB,AB6030 AB6, AA1030 AA, SA1030 SA
WHERE   ABB.D_E_L_E_T_ = '' 
AND AB7.D_E_L_E_T_ ='' 
AND AB6.D_E_L_E_T_ = '' 
AND SA.D_E_L_E_T_ = '' 
AND ABB.ABB_FILIAL = AB6.AB6_FILIAL 
AND ABB.ABB_NUMOS = AB6.AB6_NUMOS 
AND ABB.ABB_FILIAL = AB7.AB7_FILIAL
AND ABB.ABB_NUMOS = AB7.AB7_NUMOS 
AND AB6.AB6_CODCLI = SA.A1_COD
AND AA.AA1_CODTEC = ABB_CODTEC
AND ABB.ABB_CODTEC = '".$var1."'  
AND AB7.AB7_TIPO IN ('1','3')   
        AND AB7_FILIAL = '99'";

$stmt = sqlsrv_query($conn, $sql);
$row_count = sqlsrv_num_rows($stmt);

if ($row_count > 0 ){
    $_SESSION['codMont'] = $var1;
    header('location:homee.php');
}else{
    unset ($_SESSION['codMont']);
    $mensagem = "<div class='alert alert-danger'>Código do Montador ou senha incorretos. Tente novamente!</div>";
    printf ($mensagem);
    printf ($var1);
}

Even if I put the correct or incorrect code does not work, it always says that it does not have a register with this code, but it does. Does anyone know what the problem is?

    
asked by anonymous 08.06.2018 / 13:28

1 answer

0

This occurs because it uses SQLSRV_CURSOR_FORWARD , as link , as the link itself indicates:

  

sqlsrv_num_rows returns an error for result sets created with this cursor type.

     

Translating: sqlsrv_num_rows returns an error for the set result returns an error for result sets created with this cursor type. (referring to SQLSRV_CURSOR_FORWARD )

This must be set to sql_query , and the following parameters are supported for Scrollable that must be set in the third parameter, $options , as below:

sqlsrv_query ($conn, $sql, array $params, array $options)

The options for Scrollable are:

  • SQLSRV_CURSOR_FORWARD (default)

    Allows you to move one line at a time, starting at the first row of the result set, until you reach the end of the result set.

  • SQLSRV_CURSOR_STATIC

    Allows you to access rows in any order, but will not reflect changes in the database.

  • SQLSRV_CURSOR_DYNAMIC

    Allows you to access rows in any order and reflects changes in the database.

  • SQLSRV_CURSOR_KEYSET

    Lets you access rows in any order. However, a key set cursor does not update the row count if a row is deleted from the table (a deleted row is returned with no values).

  • SQLSRV_CURSOR_CLIENT_BUFFERED , it is not informed in php.net ( link ), but as php.net itself indicates the microsoft site so I believe it works on all current versions (I'll detail soon)

    Lets you access rows in any order. It creates a client-side cursor query (client side in case it refers to PHP, ie PHP is the bank server's client, there is not be with front-end, html and css, the process is in PHP itself, the other cursors other than this side of the bank)

Whenever possible, look at the documentation, there are sessions for examples of almost all APIs, like this link

For your case it should look something like:

$params = array();
$options =  array( 'Scrollable' => SQLSRV_CURSOR_KEYSET );

$stmt = sqlsrv_query($conn, $sql, $params, $options);
$row_count = sqlsrv_num_rows($stmt);

if ($row_count > 0 ){
    $_SESSION['codMont'] = $var1;
    header('location:homee.php');
}else{
    unset ($_SESSION['codMont']);
    $mensagem = "<div class='alert alert-danger'>Código do Montador ou senha incorretos. Tente novamente!</div>";
    printf ($mensagem);
    printf ($var1);
}
  

Note: sqlsrv_num_rows can return 0 or FALSE , ie it will depend on your need, in case FALSE is when something fails, and 0 (zero) occurs when you find zero records, so in your specific case check if it is greater than zero > 0 will work for both zero and false, but in other cases this may not be ideal, even more so if you need to know what may have happened, for example:

if ($row_count === false) {
    echo 'Erro ao obter o total';
} else {
    echo "Foram encontrados $row_count resultado(s)";
}
    
13.06.2018 / 03:03