Very confused and have probably overlooked something, but some ideas would be appreciated!
I have a query as follows:
$usernamequery = "select username + ' ' + surname as username, userid from users where username + ' ' + surname = '$username'";
$usernamestmt = sqlsrv_query( $conn, $usernamequery);
if( $usernamestmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while( $obj = sqlsrv_fetch_object( $usernamestmt))
{
echo $username1 = $obj->username;
echo $userid = $obj->userid;
}
which doesn't return anything, however when I echo out the $usernamequery I get
select username + ' ' + surname as username, userid from users where username + ' ' + surname = 'Joe Bloggs'
When I then go and run that directly in SQL it returns the results I'm expecting.
What's more odd is that when I then change the PHP to the actual query (i.e.
select username + ' ' + surname as username, userid from users where username + ' ' + surname = 'Joe Bloggs'
)
it runs like a charm and returns the results I'm expecting.
All in all, I'm dead confused...!
$usernamevariable right into your SQL like a caveman. Use a place holder andsqlsrv_prepare()!sqlsrv_query()and there's an example in the manual page for that funciton.