SQL - Select doesn't retrieve results

后端 未结 1 465
攒了一身酷
攒了一身酷 2021-01-25 03:20

I\'m using sqlsrv_num_rows in order to check if a user exists in the DB.

When i\'m running the query in my DB i\'m getting 1 result, but in my PHP I\'m not

1条回答
  •  花落未央
    2021-01-25 04:16

    Explanations:

    • Function sqlsrv_num_rows() requires a client-side, static, or keyset cursor, and will return false if you use a forward cursor or a dynamic cursor (the default cursor is forward cursor). Execute sqlsrv_query() with additional $options parameter and set the appropriate cursor type with "Scrollable" => SQLSRV_CURSOR_KEYSET
    • Use parameterized statements. Function sqlsrv_query() does both statement preparation and statement execution and can be used to execute parameterized queries.
    • If you want to check if the result set has one or more rows, you may use sqlsrv_has_rows().

    Example, based on your code:

     SQLSRV_CURSOR_KEYSET);
    $stmt = sqlsrv_query( $conn, $query, $params, $options);      
    if ($exec === false){
        echo print_r( sqlsrv_errors()); 
        echo "
    "; return (false); } $count = sqlsrv_num_rows($stmt); if ($count === false) { echo print_r( sqlsrv_errors()); echo "
    "; return (false); } else { echo "num: ".$count; } ?>

    Notes:

    Do not send user credentials in plain text.

    0 讨论(0)
提交回复
热议问题