0

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.

Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
Alen
  • 897
  • 2
  • 15
  • 33
  • 2
    If your tutorial teaches you to use `mysql_*` function, that may be a hint that the tutorial is a little bit outdated; Use [`mysqli`](http://www.php.net/manual/en/book.mysqli.php), especially [prepared statement and variable binding](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – Passerby Jul 26 '13 at 09:15
  • remove single qoutes in user_id, users and username. – shark Jul 26 '13 at 09:15

5 Answers5

0

use " insteand of '

"SELECT user_id FROM users WHERE username = '".$username."'"
Nightmare
  • 251
  • 1
  • 5
0
return mysql_result(mysql_query("SELECT 'user_id' FROM 'users' WHERE 'username' = '".$username."'"), 0, 'user_id');
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
user2622044
  • 185
  • 3
  • 4
  • 14
0

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'];    
}
Paddyd
  • 1,870
  • 16
  • 26
0

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 . '";')
Macbric
  • 472
  • 4
  • 10
  • All of those answers worked but I choose this one, because it's simplest. Thank you. – Alen Jul 26 '13 at 13:17
0
WHERE 'username' = '".$username."'"   replace this in query and try again
Vikas Gautam
  • 997
  • 7
  • 23