The best way I learned was to figure out some type of project you want to develop. Choose something simple like a user login system. You from there can begin to understand the basics of PHP and MySQL.
Here is a tutorial.
First part of it is the database. You need a brief understanding of a database and how its setup. Since you are not deploying heave performance driven applications, simple make a database called mylogin (if your host is shared on cpanel, the database name is sometimes something like yourusername_mylogin). Once that is created then create a table called users. In the users table, make 3 fields: id, username, and password. The ID can be a TINYINT(2), Primary Key, and auto_increment and the other two can be VARCHAR(32). Also, make sure you add a record into that table with a username and password.
Now for the script. None of these are tested or written to standard so give me a break 
First, you need to have a login form. Create a page called login.html with this code.
Code:
<html>
<body>
<center><h2>Please login</h2></center>
<form name="login" method="POST" action="login.php">
Username: <input type="text" name="username"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="Login" name="do_login">
</form>
</body>
</html>
Basically that is a simple HTML form with names on all the fields so we can access them in the PHP hander script. Also, for security reasons, your form method should always be POST unless you want the variable posted to the URL. Sometimes later you can use GET to handle certain page aspects but for now, leave it at POST.
Now, you need to handler the form some how. In the FORM tag, we have action="login.php". This means that the form will send it self to that page. Lets begin to code that page.
Save this page now as login.php
Code:
<?
// First lets check to see if the form was submitted. This prevents someone from going to this page directly.
if(isset($_POST['do_login'])){
// It is so were now in here
// Were gonna change the variables from $_POST to local ones for ease of coding.
$username = $_POST['username'];
$password = $_POST['password'];
// We also need to setup our mysql connection
$mysqluser = "username_username";
$mysqlpass = "password";
$mysqlhost = "localhost";
$mysqldb = "mylogin"
mysql_connect($mysqlhost, $mysqluser, $mysqlpass) or die(mysql_error());
mysql_select_db($mysqldb) or die(mysql_error());
//Now we can connect to the database, lets try to see if we can login.
//First select all records in the table with the username and password that match the one entered.
$query = mysql_query("SELECT * FROM users WHERE username='$username' && password='$password") or die(mysql_error());
// If the username and password was correct then there would be 1 row from the table returned. If one was wrong then there would be 0.
$rows = mysql_num_rows($query);
if($rows == 0){
echo "<center>Invalid username/password</center>";
} else {
echo "<center>You have entered a correct username and password.</center>";
}
}
?>
Thats it! Now you can expand on that and carry that login through differnet pages using some more features (pretty simple). I hope that helps. Next work on adding and removing data from a database. I can write another tutorial for that one if you would like.
Bookmarks