Click here to Skip to main content
15,916,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning how to use PHP with MSSQL server but having trouble doing a simple query. I have the below code which the goal is to be a simple login page. However it is currently just trying to get the row count when executed. I am getting my echo'd "Error getting row count" message when I would be expecting "2". I'm not getting any errors with the connection($db) or the statement($stmt).

Any help is appreciated!

include 'config.php';
$errorMsg = "";
$validUser = $_SESSION["login"] === true;
if(isset($_POST["sub"])) {
/* Connect using SQL Server Authentication. */  

// $subUsername = $_POST["username"];
// $subPassword = $_POST["password"];
//Check Connection
if( $db === false ) {
     die( print_r( sqlsrv_errors(), true));

$sql = "SELECT pin FROM user_db WHERE username like 'Cody%'";

$stmt = sqlsrv_query($db, $sql);

if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));


$row_count = sqlsrv_num_rows($stmt);
if ($row_count === false)
echo 'Error getting row count';
   echo $row_count;

if($validUser) {
   header("Location: /index.php"); die();
<!DOCTYPE html>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <form name="input" action="" method="post">
    <label for="username">Username:</label><input type="text" value="<?= $_POST["username"] ?>" id="username" name="username" />
    <label for="password">Password:</label><input type="password" value="" id="password" name="password" />
    <div class="error"><?= $errorMsg ?></div>
    <input type="submit" value="Home" name="sub" />

What I have tried:

Originally I was trying to get the row count using parameters in my statement but wasn't getting any results. So instead, I have converted it to see if I can just get a row count but still having trouble.
Updated 15-May-23 9:59am
0x01AA 15-May-23 15:10pm    
From PHP: sqlsrv_num_rows - Manual[^]
sqlsrv_num_rows, Return Values
Returns the number of rows retrieved on success and false if an error occurred. If a forward cursor (the default) or dynamic cursor is used, false is returned.
Cody O'Meara 15-May-23 15:40pm    
Thank you! I should read better. This resource helped me to resolve
0x01AA 15-May-23 16:02pm    
You are welcome

1 solution

Looks like OP solved it.
Only answered to remove it from unanswered questions.

According to OP, that piece of text was helpfull: Cursor Types (SQLSRV Driver) - PHP drivers for SQL Server | Microsoft Learn[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900