简体   繁体   English

PHP的MSSQL变量不起作用?

[英]PHP MSSQL variable not working?

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 这不会返回任何内容,但是当我回显$ usernamequery时,我得到了

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. 然后,当我直接在SQL中运行该代码时,它将返回我期望的结果。

What's more odd is that when I then change the PHP to the actual query (ie 更奇怪的是,当我随后将PHP更改为实际查询时(即

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...! 总而言之,我感到困惑……!

My educated guess is that $username does not contain what you think it does. 我的有根据的猜测是$username不包含您认为的内容。 It possibly has a tabulator or more than one spaces, but you're possibly echoing it into HTML and the browser is collapsing blank space into a single space. 它可能具有制表符或不止一个空格,但您可能会将其回显为HTML,并且浏览器正在将空格折叠为一个空格。

You can inspect the exact contents of a variable with var_dump() , eg: 您可以使用var_dump()检查变量的确切内容,例如:

var_dump($username);

... of, if you need further details, with bin2hex() ; ... of,如果您需要更多详细信息,请使用bin2hex() ;

var_dump(bin2hex($username));

将查询更改为

$usernamequery = "select username + ' ' + surname as fullname, userid from users where fullname = '$username'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM