SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    England
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to write a function to check if something exists in a DB...

    What I want to do is be able to have a function called 'isadmin()' and what it does is, it has two arguments, first: username, second: password, and it takes these arguments and selects 'userid' from $adminstable WHERE username = '$username' AND password = '$password' and if a record exists for this username and password, return true, if not, return false.

    I'm not sure how to go about this, how could I do this? Many thanks in advance.

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not complete, so you fill in the details:
    PHP Code:
    function isAdmin($username$password)
    {
      
    // establish db connection
      // build query 
      
    $query "SELECT userId FROM adminTable
                  WHERE
                    userName = '
    $userName' AND
                    password = '
    $password'
                    "
    ;
      if(
    $result mysql_query($query))
        return 
    true;
      else
        return 
    false;

    HTH
    John

  3. #3
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    England
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks John, just one question, if I've connected to the DB in the same file as I place the function, but not in the function, do I still need to include the connection?

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You would need to pass the connection string to the function, unless you declared the connection string as "global" within the function.

    "global $connectionString;"
    John

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It also depends if you have more than 1 connection or not.

    mysql_query() defaults to using the last connection opened.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    England
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just have one connection in the file.

    So all I need to do is put global $connect; in the function?

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only have 1 you don't need anything.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


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
  •