View Full Version : Php Scripting... BASIC - INTERMEDIATE... (includes full mysql tutorial)


Mushroom5698
05-09-2004, 04:15 AM
Guide to this tutorial...

Reading this guide, it is assumed that you know absoutely NOTHING about php scripting...

Table of Contents:

Chapter 1. How to run a PHP script...
Chapter 2. The most basic of PHP scripting...
Chapter 3. Randomize...
Chapter 4. If/elseif/else commands... Allows your script to check stuff...
Chapter 5. The harder stuff... MYSQL! (full tutorial on EVERYTHING mysql
Chapter 6. Ill think of what I want for chapter 6, later, and when I do, ill post it.


Chapter 1 - How to run a PHP script...

Ok, if you are totally clueless on what a PHP script is, does, and how to use it, read this...

A PHP script IS a server side scripting language... In english, this meens that the person viewing the website won't get to see whats happening, unless you allow a way for them to see it...

A PHP script DOES just about anything you want it too...

To use a PHP script you must have a website... This website must have PHP enabled on it. If you are not sure if your website has php enabled, contact your provider... To use it you must upload a .php file to the server, and than run it in your browser.

Any questions? If so, please email me at Mushroom5698@aol.com and ill reply... If not, move on...

Chapter 2 - The most basic of PHP scripting...

This is the MOST BASIC script you could ever make... Ill go over it step by step in just a few seconeds...

<?php
echo("Hello, World!");
?>

On your computer, goto start, run, and type notepad. Copy and paste this code in notepad. Save the file as "hi.php" (include the quotes!). Upload this file to your website, and view it in your browser... Do you get what it does? If not, this is how it works...

<?php - This starts your PHP script... EVERY php script you make, will begin with this...

echo("Hello, World"); - this prints "Hello, World" in the browser

?> - This ends your PHP script... EVERY php script you make, will end with this...

Get it? If not please email me at Mushroom5698@aol.com, and I will help you with what ever you don't understand... If you understand it, go on...

Another basic thing in a PHP script you should know, is making notes... Here is a PHP script...

<?php
//This is a note
?>
What does this script do? Nothing... But, lets say you write a PHP script, and your not sure if you'll remember what everything does... Just put notes everywhere in the script, about what the next line does...

So, that is very basic stuff, yet, if you do not get it, send me an email!

Chapter 3 - Randomize!

Ok, it seems like there are SOOOO many people who want to know how to randomize stuff! Well, heres a very simple way...

Firstly, let me introduce arrays. Arrays, are just more than one value assigned to one variable... OH! I forgot to tell you how to make variables...
Ok, lets say you wanted to make a variable...

$hi = "hello";

Simple, right? That meens that whenever you typed $hi it would return hello... An example...

<?php
$hi = "hello, world";
echo "This is an example of a variable: $hi";
?>

Would print on the screen: This is an example of a variable: hello, world

Get it? If not email me!

Now, back to arrays... Lets say you wanted to store a list of colors, in one variable... You have to make an array, useing the FUNCTION array...

$colors = array("black","blue","green","red","yellow");

So, whenever you reference to $colors[0] you would get black... $colors[1] would be blue, $colors[2] would be green, and so on...

So, lets say we wanted to get a random color, and print it to the screen...
First we have to learn something new! The funcion array_rand

array_rand
Here is a sample code for it...

array_rand($colors, 1);

The $colors part tells it that the array were dealing with is called "colors"... The part the say 1, is telling it to pick out ONE random choice... This function will only return a number, however... We have to put that number in the array... At this point, it gets a little more complicated, but ill explain it to the best of my ability...

<?php
// Create an array stored in the variable called "colors"
$colors = array("black","blue","yellow","white");
// Get 1 random part of the array... (this will return 0 1 2 or 3)
$random_number = array_rand($colors,1);
// This is where it gets a little tricky... You remember how I said that if you
// Wanted to reference the "blue" in this array, you would have to reference
// $colors[1]..? Well, with this random thing its the same... Only, instead of
// saying one, we will reference $colors[$random_number]
// and store it in a variable called $result
$result = $colors[$random_number];
echo $result
?>


Thats all 4 this chapter... If you have any questions, email me at Mushroom5698@aol.com... Ill answer them all to the best of my ability...

Ill write the chapter on Mysql tomorrow (may take a few hours to write... SOOOO complicated, in some areas of mysql...)

Mushroom5698

Mushroom5698
05-09-2004, 01:44 PM
Chapter 4 - If elseif else statements...

Ok, this chapter is going to be quite long... Because I have TONS of information I'm gonna stuff into it... Ill explain it all to the best of my ability...

Now, lets assign to variables....

$variable_1 = "hello";
$variable_2 = "hi";

Now, lets pretend that you do not know what is assigned to these variables... But you want to check... You would use the if/else command...

if ($variable_1 == $variable_2) {
echo("variable 1 is equal to variable 2!");
} else {
echo("they are not the same");
}

Lets go over this step by step...

if ($variable_1 == $variable_2) { - This is like saying "If $variable_1 is equal to $variable 2 then...

echo("variable 1 is equal to variable 2!"); - you know what this does...

} else { - closes up the first command and starts another... this is like saying...
"if this is not true, then..."

you know what all the rest does.. But here, to make it eaiser ill write it all out in english language...
if $variable 1 is equal to $variable to then display the text "variable 1 is equal to variable 2!" but do nothing else. If this is not true, then display the text "they are not the same"



Also, if you don't get it, try looking at this...

if (CONDITION) {
//action to preform if the condition returns true, or as a matter of fact is true.
//if you put 4 < 6 in the condition part, it would return true... but, if you put
// 4 > 6 in there, it would return false, because that is not true, is it?

// This bracket you see bellow is telling the script to stop it all! That this is
// done with, and to move on...
}
// this else is telling the computer to ONLY PREFORM IT if the condition
// returns false... if you put 4 < 6 in the condition, than the part under else
// would not be preformed... if you put 4 > 6 under it, than the else part
// would be preformed.
else
//this tells it that it is the start of the code to be preformed...
{
// WHAT TO DO IF THE CONDTION RETURNS FALSE GOES HERE
}



Now, like I said this chapter is going to be loooonnnggg... So I MUST go on...

Now, lets go into HTML for a while... If you have read LissaExplains HTML guide, you probally know how to make forms... If not, click here (http://lissaexplains.com/html4.shtml#form) for a very BAISC understanding of forms... Not, first I'm going to tell you that were gonna need to files to execute this script... a .html file and a .php file...

Name the .php file "get_result.php" - make sure you include the quotes...
If you read up on forms, you should know what this will meen... In the HTML file put...

<form action="get_result.php" method="post"><b>
Username, please: <input type="text" name="username"><br>
Password, please <input type="text" name="password"><br>
<input type="submit" value="Login, now!"></form>

Just incase you don't know what this meens, I will say it in english...

We are using a form that will POST to get_result.php. We display Username Please, Then have an input field that is named username for the next page. We have a line break. We put Password, please on the page, then have an input field name password for the next page... Line break. We have an input button that say Login, now! in it...

Now, lets work on the PHP script...
There is nothing new we will have to address execpt... That in this form, it will send the variable $username and $password to our script... $username will be what was inputed in the username box, and $password will be what was in the password box...

<?php
$correct_username = "Mushroom5698";
$correct_password = "23gg2gfhsd";
// Here is one thing that is new... You can check TWO things in one if
//statement... you do this by if ((CONDTION 1) && (CONDITION 2)) {
if (($username == $correct_username) && ($password == $correct_password)) {
echo("Access is granted. Welcome, administrator.");
} else {
echo("Access denied.");
}
?>

Ok, so this chapter isen't as long as I thought... But itst still long...

Mushroom5698
05-19-2004, 01:20 AM
Ok, now this is where it can get difficult.
Firstly, this is what is required to begin this mysql tutorial...

1. A Mysql Database
2. (A php enabled website)
3. PHP my admin

Ok, so go into your PHP my admin and create a database, along with a table called users. In the table users, add the rows "username", and "password".
What you just did is create a storage area.
Now, in the rows insert a new field with the username be "hello" and the password being "goodbye"

Now, start a php file and put this code in it... Don't worry about what it does, ill explain it later.


$right = 0
$connect = mysql_connect("DB NAME","DB PASSWORD","host(usualy localhost)");
$select = mysql_select_db("DATABASE NAME");
$query = "select * from username where username = '$username' and password='$password'";
while ($a == mysql_fetch_array($result)) {
if (($a["username"] == "$username") && ($a["password"] == "$password")) {
$right = 1
}
}
if ($right == 1) {
echo("Correct. Welcome, administrator");
} else {
echo("Incorrect. Access is Denied to user $username using password $password attempting to access secure area");
}

Line by line...

//ASIGNS THE INTEGER 0 to the VARIABLE $right
$right = 0
//CONNECTS TO A MYSQL DATABASE USING THAT USERNAME, PASSWORD,
// and HOST
$connect = mysql_connect("DB NAME","DB PASSWORD","host(usualy localhost)");
//SELECTS THAT DATABASE NAME
$select = mysql_select_db("DATABASE NAME");
// SETS THE QUERY VARIABLE TELLING IT TO SELECT STUFF FROM
// THE DATABASE WHERE THE USERNAME IS THE SAME AS THE VARIBALE
// USERNAME AND THE PASSWORD IS THE SAME AS THE VARIABLE
// PASSWORD
$qquery = "select * from username where username = '$username' and password='$password'";
// SETS THE VARIBLE QUERY WITH THE QUERY
$query = mysql_query($qquery);
// CHECKS TO SEE IF THERE IS A MATCH. IF THERE IS, RIGHT IS GREATER
// THAN 0
while ($a == mysql_fetch_array($result)) {
$right++;
}
if ($right > 0) {
echo("Correct. Welcome, administrator");
} else {
echo("Incorrect. Access is Denied to user $username using password $password attempting to access secure area");
}



Soon Ill tell how to allow users to register an stuff like that but im kind of busy right now, very sorry about that.

Mushroom5698
05-19-2004, 01:22 PM
I got some emails saying that people coulden't understand that MySql Tutorial, so Im gonna go over it... Step by step. Last night I was kind of busy So I Couldent really go into detail, but I will now...

Most hosts provide something called CPanelX with their website. Go into your websites Control Pannel (usually www.yourname.com/cpanel) and click on "Manage Mysql" (May be a slight variation of what it says, but it should be similar to that... If your cpanel doesen't have that, email me and I will help you out)

On That page there should be something that says "Db: " and than a form field. In that form field type... As an example "comments". The cpanel will format the database to be named THE-USERNAME-OF-YOUR-CPANEL_comments

Say, when you logged into your control pannel, your username was... lissa, and you created a database "comments". The database would be called lissa_comments. If your cpanel username was joe, it would be joe_comments. Get it? If not, email me at Mushroom5698@aol.com

So, you've created that database USERNAME_comments. Now we need to add a user to that database. Return to that page your were just on. You should seee a form field "Username" and "Password". Fill them both in. Lets say you made the username "Robert". The actual username would be lissa_robert or joe_robert... USERNAME_robert. So add a user to the database.

Once the user is added, we can start our script...

First, we need to make a connection to the database. There is a very simple way to do this. Just to note, the usual host for a database is "localhost". If it doesen't work for you, contact your website host.

The code to connect to the database is...

$connect = mysql_connect("Username","Password","Host");

As an example. Say my username was joe_robert and my password was 84645 and my host was localhost... This would be the code...

$connect = mysql_connect("joe_robert","84645","localhost");

Well, we have our connections established (if it dident work it will return an error), but the website doesent yet know what database we are using... So we need to tell it...

$database = mysql_select_db("DATABASE_NAME");

So, if our database name was joe_comments, this is what the code would be...

$database = mysql_select_db("joe_comments");

So, now we have a connection to the database. Were all ready to execute the mysql code. I guess that first Ill teach you how to put stuff inside the database...

To execute a mysql query, you use the function... mysql_query(QUERY HERE);
But i usually store my query inside a variable... So lets start up a query on inserting stuff into it. But, first we need to make some tables...

Go into your websites control pannel, and go to PHP my admin (can be found in manage mysql than click on PHP my admin)

Go to the list on the side and choose your database... Where it says Create a new table, make the tables name "users" and the number of fields, 2.

Continue. You will get to an area where there are two form fields. Make the first one say "username" and the seconed "password".

Can you guess what we are doing...? Were making a place where users can... Register & login! Also, change the type of field it is to a "TEXT" field.

Right now we won't worry about encrypting passwords, ok? Not to mention that the encryption is SO eaisly broken...

Ok, now we have our fields... So we will create the code. Make a code where we connect to your mysql host, than select the database. Now make a query variable...

$query = "INSERT INTO users ( `username` , `password` )
VALUES (
'$username', '$password'";

"INSERT INTO" tells the website that we are inserting something into the database. " users " is telling the website to select it from the table users. `username` tells it to insert into the username field and `password` tells it to insert into the password field (Please note; Do not user ' or " you must use ` which is left of the one on your keyboard.

Than, run the query.

$q = mysql_query($query);

Now assign $username and $password with a variable, than go to PHP my Admin, and click on Browse. You should see whatever you assigned to the variables username and password! Now, Ill go into getting the variables out of the database later. Right now I'm in school (Only 4 days left! Yay!) and I have to go to class. Ill tell yall stuff later.

If you have any questions email me at Mushroom5698@aol.com

Mushroom5698