View Full Version : Learning db

04-20-2004, 09:51 PM
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 :eek:

04-20-2004, 10:05 PM
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.

04-20-2004, 10:19 PM
I mean a login (username/password) database. That just searches through the database to see if they exist...

04-20-2004, 11:12 PM
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.

// Start a session so we can access session variables

//Connect to the database

// Try to match presented credentials to those stored in database
$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');
case 'Borrower' :
header('Location: BorrowerView.php');
case 'Originator' :
header('Location: OriginatorView.php');
} //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';

04-21-2004, 01:27 AM
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:

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:

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:

$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,