Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Learning db

  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning db

    I want to learn how databases work (particulary login scipts) but I have NO idea how they work and the tutorials at php freaks are too advanced for me. Does anyone know more basic tutorials

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well your question is kind of vague. There are many different database systems and there are also many different areas of database development. What specifically are you interested in? The most commonly used database with PHP is MySQL which can be found at www.mysql.com. If you are wanting to learn how to design a database you will probably want to get familiar with SQL which is the language used in running queries against the database. You can learn about that at www.sqlcourse.com and www.sqlcourse2.com. As for proper database design and development there is quite a bit involved in that such as learning how to structure your database and normalizing it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I mean a login (username/password) database. That just searches through the database to see if they exist...

  • #4
    New Coder
    Join Date
    Apr 2004
    Location
    Texas
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here's an example of the code I post my credentials to, if it helps, but spookster is right, you need to do some reading first....

    Spookster - that sqlcourse2....nice link, I learned a lot, I've been using more basic queries until now.

    PHP Code:
    <?php
    // Start a session so we can access session variables
    session_start();

    //Connect to the database
    include('Connect.php');

    // Try to match presented credentials to those stored in database
    $pass=$_POST['Password'];
    $query="select * from users where UserID='".$_POST['UserID']."' and Password=PASSWORD('$pass')";
    $result = @mysql_query($query);
    $num_results = @mysql_num_rows($result);

    if (
    $num_results>0// If we have a match, then the User is authenticated
    {
        
    $row mysql_fetch_array($result);
        
    $_SESSION['Authenticated'] = TRUE;
        
    $_SESSION['UserID'] = stripslashes($row['UserID']);
                 
    $_SESSION['RealName'] = stripslashes($row['RealName']);
        
    $_SESSION['UserType'] = stripslashes($row['UserType']);
        
    $_SESSION['OriginatorID'] = stripslashes($row['OriginatorID']);
    }

    if (isset(
    $_SESSION['Authenticated'])) //If authenticated variable exists
    {
        if (
    $_SESSION['Authenticated']== TRUE
                     
    //If authenticated variable is TRUE
        
    {
            switch (
    $_SESSION['UserType']) 
                             
    //Send user to appropriate page for his UserType
            
    {
                    case 
    'Admin' :
                                
    header('Location: AdminMainPage.php');
                            break;
            case 
    'Borrower' :
                            
    header('Location: BorrowerView.php');
                                             break;
                        case 
    'Originator' :
                            
    header('Location: OriginatorView.php');
                                             break;
            } 
    //end switch
        
    //end if authenticated = TRUE
    //end if authenticated variable exists
    else // The user isn't authenticated
    {
        echo 
    'Sorry, but you did not authenticate successfully';
    }
    ?>
    Last edited by black3842; 04-21-2004 at 12:51 AM.
    Jason B

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I will also agree with Spookster, that you should do some reading about databases.

    There are different types of databases (aside from just vendors), but the most common is the Relational Database.

    The relational database basically says there are a bunch of tables that are related to one another.

    Each table has columns and rows. A single row is called a record. A record has columns, also known as attributes.

    Typically, in a login screen, you only REALLY need 1 table. This table really only needs 2 columns - userid or username (or both) and password.

    You may feel that at some point you'd like to retain more information about your users, such as multiple emails. In that case, you might create a new table called Emails, with 2 columns (or maybe 3..) - userid and email (and maybe emailid..). In this way your Users table and your Emails table are 'related' by the userid.

    So if you've got 2 users with 2 email addresses, this is what your tables could look like:
    Code:
    Users Table
    userId userPass
    1      'secret'
    2      'pass'
    
    Emails Table
    userId emailAddress
    1      'email1@hotmail.com'
    2      'email2@hotmail.com'
    1      'email3@hotmail.com'
    2      'email4@hotmail.com'
    So now you can do a query for all email addresses for userId = 1. In fact your SQL query would look like:
    Code:
    SELECT emailAddress FROM Emails WHERE userId = 1;
    Some PHP code to see if a particular user exists in your Users table could look as follows:
    PHP Code:
    $db mysql_connect('localhost''mysql_user''mysql_password');
    mysql_select_db('login_db'$db);
    $result mysql_query('SELECT * FROM Users WHERE userId='.$userId.' AND userPass=\''.$userPass.'\''$db);
    if(
    mysql_num_rows($result) > 0) {
     echo 
    "You entered a correct userId / userPass combo!";
    }
    else {
     echo 
    "You did not enter a correct userId / userPass combo. Please try again.";

    I think that should work, and make sense. But if not post back and I'll try to help you. Perhaps after reading this, those tutorials at PHPFreaks won't be so bad!

    Good luck,
    Sadiq.


  •  

    Posting Permissions

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