SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed, function not working

    I am wondering if anyone can help tell me why the second function, getByUsername, does not work when the other function works perfectly. I have listed the code below, thanks in advance!

    PHP Code:
    //return an object populated based on the record's user id
    public static function getById($user_id)
    {
        
    $user = new User();
        
    $query sprintf('SELECT FIRST_NAME, LAST_NAME,   USERNAME, PASSWORD, EMAIL_ADDR IS_ACTIVE ' 'FROM %sUSERS WHERE USER_ID = %d'DB_TBL_PREFIX$user_id);

        
    $result mysql_query($query$GLOBALS['DB']);
        if (
    mysql_num_rows($result))
        {
        
    $row mysql_fetch_assoc($result);
        
    $user->first_name $row['FIRST_NAME'];
        
    $user->last_name $row['LAST_NAME'];
        
    $user->username $row['USERNAME'];
        
    $user->password $row['PASSWORD'];
        
    $user->emailAddr $row['EMAIL_ADDR'];
        
    $user->isActive $row['IS_ACTIVE'];
        
    $user->uid $user_id;
         }
        
        
    mysql_free_result($result);
        return 
    $user;
    }


    //retun an object populated based on the record's username
    public static function getByUsername($username)
    {
        
    $user = new User();
        
    $query sprintf('SELECT USER_ID, FIRST_NAME, LAST_NAME, PASSWORD, EMAIL_ADDR, IS_ACTIVE ' .  'FROM %sUSERS WHERE USERNAME = "s%"'DB_TBL_PREFIXmysql_real_escape_string($username$GLOBALS['DB']));

    $result mysql_query($query$GLOBALS['DB']);
    if (
    mysql_num_rows($result))
    {
                 
    $row mysql_fetch_assoc($result);
        
    $user->first_name $row['FIRST_NAME'];
        
    $user->last_name $row['LAST_NAME'];
        
    $user->username $username;
        
    $user->password $row['PASSWORD'];    
        
    $user->emailAddr $row['EMAIL_ADDR'];
        
    $user->isActive $row['IS_ACTIVE'];
        
    $user->uid $row['USER_ID'];
    }
    mysql_free_result($result);
    return 
    $user;


  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    WHERE USERNAME = "s%"

    See the error?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, got it. That is why I love these forums. I was coming back to post my error message and I already had the answer. Thank you!

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do believe that

    PHP Code:
    $result mysql_query($query$GLOBALS['DB']); 
    Should return a resource ID, so just to be on the safe side I'd do something like

    PHP Code:
    if( is_resource$result ) ) { 
    // do more stuff
    } else { echo mysql_error(); } 
    So you can actually see the error being output.
    Cross browser css bugs

    Dan Schulz you will be missed


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
  •