right - what's happening is that you're getting an error, but you're not checking for it. Even if you fix this, you'll have the same problem if you ever ask for a username that doesn't exist (and therefore, no rows are returned).
You need to check for your result before you try to use it:
PHP Code:
function user_id_from_username( $username ){
$username = sanitize( $username );
// do the query first
$result = mysql_query( "SELECT `user_id` FROM `users` WHERE `username` = '$username'" );
// if there was an error, or no results, $result will be FALSE.
// if $result is not false, then you know there was a row returned
if( $result ){ return mysql_result( $result,0 ); }
// if you get to this point, that means there was no result.
// you could also return something like "Guest" or "Unknown User" instead.
return false;
}
--------------------------------------------------
# If at all possible, you should avoid using the mysql_* functions. #
ext/mysql is outdated and scheduled for deprecation. It is no longer recommended for new projects, and existing code should be updated to avoid performance and security problems. Using ext/mysqli or the PDO class is recommended. Read more about choosing an API here.
Bookmarks