PHP / MSSQL ..根据用户名选择ID,根据ID插入表格?

I have looked all over and at tons of code and examples.. This is such a small bit of code but I just can't seem to get it to work.

I have dbo.accounts which contains the id, username, password, createtime..

I have a simple form, you type in the username, and I need the select query to return the ID based on the username.

$result = mssql_query('SELECT id FROM dbo.account WHERE name = $username');

The dbo.gamemoney table will just insert some hardcoded info such as an amount of coins for the game..

My problem is that if I use a query as ID = 123, it works, but when I try to grab the id of dbo.accounts by using the username, I get nothing back.

I know it has to be something small, But I have tried to figure it out for so many hours now that I'm honestly lost..

Thanks for your time, Chris

Since, $username is string type, you have to enclose it in quotes.

$result = mssql_query("SELECT id FROM dbo.account WHERE name = '$username'");

As a better practice would suggest use a try-catch scenario so that you get the exact error log. Try -

$result = mssql_query('SELECT id FROM dbo.account WHERE name = "'.$username.'"') or die('MSSQL error: ' . mssql_get_last_message());

Thanks everyone for the help!

I was able to get it working. Now I'll make sure it's the right way. I had forgot to add,

while($row = mssql_fetch_array($result)) {
$id = $row['id'];
$ip = $row['ip'];
}

Thats why the id was blank. I was missing some code. Chris