PHP MySql错误无法从数据库读取id [重复]
This question already has an answer here:
I'm following login/register tutorial in php and mysql. As you can see in this video: http://www.youtube.com/watch?v=JUk2e8oqOvI&list=PLE134D877783367C7
You can find function user_id_from_username on 06:28 minute.
I have exact function:
function user_id_from_username($username){
$username = sanitize($username);
return mysql_result(mysql_query("SELECT 'user_id' FROM 'users' WHERE 'username' = '$username'"), 0, 'user_id');
}
But this query never returns user_id. I tried to echo result of this query and I don't get anything. Database is ok, all other queries works fine. If I run this query in mysql workbench again it works. I don't know where is the problem. Can you help me.
</div>
Remove quotes around user_id, users and username.
Quotes are only for string
mysql_query("SELECT user_id FROM users WHERE username = '$username'")
or optimized
mysql_query('SELECT user_id FROM users WHERE username = "' . $username . '";')
use " insteand of '
"SELECT user_id FROM users WHERE username = '".$username."'"
return mysql_result(mysql_query("SELECT 'user_id' FROM 'users' WHERE 'username' = '".$username."'"), 0, 'user_id');
You need to pull the data from mysql_result.
function user_id_from_username($username){
$username = sanitize($username);
$result = mysql_result(mysql_query("SELECT 'user_id' FROM 'users' WHERE 'username' = '".$username."'"), 0, 'user_id');
$row = mysql_fetch_assoc($result);
return $row['user_id'];
}
WHERE 'username' = '".$username."'" replace this in query and try again