I'm having trouble with my SQL query running through PHP. This is my code
$username = $_POST['username'];
$SQLString = "SELECT COUNT(*) FROM dbo.Users WHERE Username = '$username'";
$result = sqlsrv_query($conn, $SQLString) or die (print_r(sqlsrv_errors(), true));
$userExists = sqlsrv_fetch_object( $result );
echo("test");
echo($userExists);
echo("test2");
This returns testtest2
, and nothing for $userExists. This seemed to work last night, and I haven't changed anything besides printing out the sqlsrv errors, but those don't return anything.
$result
returns Resource id #3
, and I'm able to run an insert later in my code.
Can anyone see why $userExists
returns nothing at all? I thought that it would have to return 0 at the very least (if the user does not exist).
EDIT
So attempting using prepared statements that chris85 recommended, and again it spits out the exact same thing. $stmt
returns Resource ID #2
, and $userExists returns absolutely nothing. No failures, no errors.
$SQLString = "SELECT COUNT(*) FROM dbo.Users WHERE Username = '$username'";
$stmt = sqlsrv_prepare( $conn, $SQLString, array( &$qty, &$id));
$userExists = sqlsrv_fetch_object( $stmt);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
echo("test");
print_r($stmt);
print_r($userExists);
echo("test2");