Results 1 to 3 of 3

Thread: PHP user_id_from_username function HELP

  1. #1
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP user_id_from_username function HELP

    Hello.

    When i try to log in it says " Warning: mysql_result(): user_id not found in MySQL result index 9 in C:\xampp\htdocs\lr\core\functions\users.php on line 10"

    How do i fix this?

    Here is the code:
    PHP Code:
    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');
       
        

    Thank you.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    why dont you try backticks insted of single quotes for username
    Code:
     return mysql_result(mysql_query("SELECT (user_id) FROM users WHERE `username` = '$username'"), 0, 'user_id');

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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,); }
        
    //  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.
    Last edited by traq; 10-18-2012 at 04:10 AM.

Similar Threads

  1. Adding javascript function within ontoggle function
    By piers in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 07-22-2009, 03:41 AM
  2. Replies: 9
    Last Post: 09-16-2008, 06:31 PM
  3. Replies: 2
    Last Post: 08-02-2007, 07:19 AM
  4. out side of function or inside of function
    By riptide in forum JavaScript
    Replies: 2
    Last Post: 07-12-2007, 05:10 PM
  5. Passing Variables Function to Function
    By jscheuer1 in forum JavaScript
    Replies: 3
    Last Post: 03-26-2005, 12:22 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •