When you start using PHP more and more, you will start to notice that storing usernames and passwords in files etc is not an easy or quick thing to do, you need to open the file, and read the file.. okay.. thats easy enough.. but what about selecting datafrom within the file. Now thats a different story.
The idea is that it isn't very easy, and if you do find a way to do it.. its probably pretty damn CPU intensive.
*Cue Databases*
Databases come in different shapes and sizes, but when you need something powerful and cheap (by cheap I mean
FREE BABY!),
MySQL is most probably the best thing out there.
How to use MySQL Databases in applications
When we want to communicate with a MySQL database, we need to set up the connection to the server. This is done simply by providing the server address and the password (in that order) to the server.
| PHP Code |
<?php
$dbserver = "localhost"; //almost always localhost
$dbuser = "mysql_username";
$dbpassword = "mysql_password";
$connect = mysql_connect($dbserver,$dbuser,$dbpassword);
?>
|
Before even starting to query the database, we need to select it!
| PHP Code |
<?php
$dbname = "my_database_name";
mysql_select_db($dbname);
?>
|
One of the main things that you are probably wondering is, "What on earth am I going to use databases for?".. well, checkout this example.
You are making a membership system for your website, your users are going to need to login. For this we need a table.
The table will have two columns:
USERNAME and
PASSWORD
Then there will be two records:
user1 with the password
test123, and
user2 with the password
test456.
Now, lets make a simple form for the user to login with.
| PHP Code |
<table>
<form action="login.php" method="post">
<tr>
<td>
username:
</td>
<td>
<input type="text" name="UID" />
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
<input type="password" name="PWD" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="sent" value="Login >>" />
</td>
</tr>
</form>
</table>
|
Now this sweet little thing will sent the values of the two fields to login.php as $_POST['UID'] and $_POST['PWD'] respectively. So when we get the values of the post sent to the page, what are we going to do with it? How are we going to use that information to get information from the database?
The answer is, using
SQL (structured query language) to communicate with the database.
An example of this is shown below. This will get the row(s) in the database where the
username column is equal to
$_POST['UID'] and where the
password column is equal to
$_POST['PWD'].
| PHP Code |
<?php
$sql = "SELECT * FROM `users` WHERE `username` = '$_POST['UID']' AND `password` = '$_POST['PWD']'";
$query = mysql_query($sql,$connect);
|
Now that the query is done, we need to take the result of that query, and put it into an array so that we can use it, or we can just count the rows.. afterall, if there was a user with that username and that password, then we would get one row returned right? If there were no users registered to the site with that username and password combination.. then we would have no rows returned.
| PHP Code |
<?php
//checking for the number of rows returned
$count_rows = mysql_num_rows($query);
//putting the result into an array
$result_array = mysql_fetch_assoc($query);
?>
|
Now we can put everything that we have used within this tutorial together to present us with the login.php.
| PHP Code |
<?php
//first of all we need to start the mysql connection
$dbserver = "localhost"; //almost always localhost
$dbuser = "mysql_username";
$dbpassword = "mysql_password";
$dbname = "my_database_name";
$connect = mysql_connect($dbserver,$dbuser,$dbpassword);
mysql_select_db($dbname);
$sql = "SELECT * FROM `users` WHERE `username` = '$_POST['UID']' AND `password` = '$_POST['PWD']'";
$query = mysql_query($sql,$connect);
$count_rows = mysql_num_rows($query);
if($count_rows == 1)
{
//then this user is valid
$result_array = mysql_fetch_assoc($query);
echo "WELCOME TO THE SITE $result_array['username']!";
}
?>
|
As you can see with the $result_array, you can access the columns of the table through the key of the array, ie $result_array['column name'].
Et voila, a quick look at databases from within PHP. There is a lot, lot more too it.. which i will talk about in later tutorials! Ciao for now!