SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Method always returning false

    Hey,

    I have a method that checks credentials and returns true if it matches.

    PHP Code:
    public function loginUser($strUsername$strPassword) {
        
    $result $this->hDB->query("SELECT firstname, lastname, username, password, email, session 
                                     FROM user 
                                     WHERE username = '
    $strUsername
                                     AND password = '
    $strPassword'");
        if (
    $row = ($result->fetch_row()) == 1) {
          
    $this->firstname $row->firstname;
          
    $this->lastname $row->lastname;
          
    $this->username $row->username;
          
    $this->passowrd $row->password;
          
    $this->email $row->email;
          
    $this->session $row->session;
          
          return 
    true;
        }
        return 
    false;
      } 
    The problem is that it always returns false. I thought fetch_row() would do the trick to check if there is a row that matches those credentials? But it's not working.

    Looking for a hand

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,604
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Try getting rid of the ==1 as the handle on the row is very unlikely to have that value.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works. I don't understand though. When I fetch a row, it's suppose to return a number?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nobody here but you knows what fetch_row() does, but it sure looks like it returns an object, not a number. Maybe you're thinking about a function or method that returns a count of how many rows the sql query produced.

  5. #5
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fetch_row() is from mysqli.

    php manual says it returns a enumerated array.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Comparing an array to integer 1 is always going to be false.

    Also, you would need to use $row[0] $row[1] etc... see the examples in the documentation. Or, see fetch_object() / fetch_assoc(), both of which are generally considered more useful than a numerically indexed array.


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
  •