PDA

View Full Version : Basic PHP & MySQL Tutorial


Kaeliah
01-16-2011, 09:13 PM
This guide is for those who know nothing or little about PHP and MySQL.

1. Hosts, Databases and Other Info

Places like Freewebs and Geocities don't support databases and PHP files, so you have to get a little creative. There are plenty of free and paid hosting out there for you to use that do support PHP/MySQL but finding them can be a hassle. So I'll give you some personal recommendations.

Free:
www.000webhost.com (http://www.000webhost.com)
Paid:
www.webhostingpad.com (http://www.webhostingpad.com)
www.godaddy.com (http://www.godaddy.com)

WBH is the only paid host I've ever used but they're extremely helpful and have tons of features for a very decent price. Anyways, my suggestion for free hosts is 00webhost but I would NOT suggest Byethost because they don't support a couple things that are required. There are others out there, these are simply the ones I'm familiar with.

Now, once you've got a host down lets cover some other basics, Databases! A database is where all the information for your site will be stored. It'll be stored in detailed tables that can be added to, deleted from and updated at pretty much any point in time. Web Hosts will have specific directions as to how to set up a database, and make sure you copy down any information, such as the Database Name, the Database User and Password and so forth. This will come in handy when connecting to the Database. Now the Rusnak Script comes with a pre-installation, so you input the information where it goes and then you'll be able to connect to your database automatically as it's already coded into each page(Just be sure to use the 'blank.php' template when making new pages). If people ask I'll show how to code a connect script but for now lets stay simple.

Once connected to a database, you can access it by coding MySQL 'Queries'. Basically your telling the database to get, add, delete or update information. There are many useful functions that can be done but for now those are the basics.

When using PHP, similar to HTML you must save it as a different document type. When saving an HTML file, you save it as .html, although if your saving a file with php you have to save it as .php. You can edit PHP code in Windows Notepad, or download a better editor. My personal favorite is Notepad++, although if you want a debugger(a check that finds mistakes in the code) PHP Designer 2007 is a pretty good program. The best way to learn any code, including PHP is to get in there and figure out what's going on with a script and try to change a couple things. So let's get to scripting.

2. PHP Basics

Before we get into MySQL let's get into some PHP basics. A quick vocab word: Syntax. Syntax refers to the grammar and structure of the code. If you have a syntax error, it's probably because you forgot a ';' or a '}' in the code. The first things you should know about PHP is the tags. You have to use these tags within a PHP file in order for the PHP to work.

<?php
PHP CODE HERE
?>Simple enough right? You can make the entire page like this or just blocks of the page. PHP is convenient in that you can make the page mostly HTML with a little PHP or the other way around. MA sites are mostly PHP with a little HTML. Next thing is variable. If your familiar with Algebra or higher then you'll already know what a variable is. Variable comes from the word vary, which means it changes, it's not always the same. Variables in PHP always start with a $ followed by a letter. A variable starting with a $_ or a $4 will not work! Some examples of variables:

$money
$pet_id
$total
$tax2You can set a value for a variable very easily. See a variable as a sort of bottle, with a little message inside. It can only hold one message, but it holds it none the less. If you say 'okay this variable equals 5,' then the little message inside the bottle is going to say 5

$money = 5;Note the syntax here. I used an equal sign to say that the variable now represents the number 5, then ended the line in a semi-colon. The semi-colon signifies the end of what that variable represents or is equal to. You can also store text in a variable.

$welcome = "Welcome to My Site!";Note that the message is in quotation marks. That's so the computer reads it as text instead of code, and then the semi-colon to end the statement. Now what can you do with these variables? A whole lot! First let's use some operators. The four basic ones; multiplication, addition, division and subtraction. Take a look at the following bit of code.


$num1 = 4;
$num2 = 8;

$num3 = $num1 + $num2;$num3 would then be set to the value of 4 + 8 which is 12. Replace the addition sign with a subtraction and the variable $num3 would be -4. And so on and so forth through the other two operators. There are several operators that work slightly differently.


$num1 = 5;
$num1++;The code above is saying that the variable equals five, and then the variable is incremented(added to) by 1. So then the variable would equal six. This also works for '--'.

Alright now that we know some basic PHP code and how to write it, lets move on to something a little more useful. If-Else Statements.


if($money == 5){
echo "You have 5!"; }
else {
echo "You don't have 5!"; }What can you gather from the code above on your own? If we translate the script into English as best we can, it says 'If the variable money is equal to 5, then print on the page "You have 5!" If it does not equal five, print on the page "You don't have 5!"' You start the code off with the if(). In the parentheses you need your requirement. For example if you were to only allow users with enough money to purchase an adopt, then you would check to see if they have enough. If they have enough, they can purchase, otherwise they get an error message. The else is not required however it is suggested in most cases. The other option is 'else if()' meaning there is a secondary check if the first one is false. So if they don't have EXACTLY the amount they need to purchase the pet, do they have more? If they do you'd still allow them to purchase the pet so you need a second check. the else then represents a situation that did not fall into any of the previous categories. The requirement itself has a certain coding. '==' means 'is equal to'. Using only one equals sign will mess up your code. Other options to use are '!=' which is not equal to, <, >, <=, and >=. Respectively they are less than, greater than, less than or equal to and greater than or equal to. You can also use these checks to identify text as well, whether something says yes, no or is enabled or whatever. Next come the brackets '{ }' Anything within those brackets will be done if the requirement is satisfied. If the requirement is not satisfied, the code within those brackets will not be used. Make sure to always have a closing bracket!! You also may notice something new: echo. Echo simply means print onto the page whatever is in the quotations.

For organization purposes, there is something called comment lines, and comment blocks. Most scripts only bother with comment lines. When you get a code that's thousands of lines long, comments are very helpful for finding what your looking for. Comment lines can say a simple statement about the code following it.

// Start log in check
$fix = 5; // This variable equals 5These lines can be placed just about anywhere including after code. The double slashes '//' will tell the computer to disregard the rest of the line. Comment blocks are multi-line comments used for heading of specific sections of code or copyrights and the like.

/*
This is
A
Comment
Block
*/Comment blocks can be as many lines as you want and the slash stars '/*' and '*/' tell the computer to disregard anything in between them.

Congratulations, you know some PHP basics!

3. MySQL Basics

MySQL goes hand in hand with PHP quite well. While there are different ways of running a query, this is the one I'm most familiar with. These queries can be written directly into the PHP code like this,

mysql_query();Now depending on what you want to do within the query, we fill in the parentheses. Lets start with a Select From.

mysql_query("SELECT * FROM tablename");That code will select every entry from 'tablename' in the database your connected to. This would be good if you were listing every user for example. However sometimes you want to select entries that are a little more specific. Say you want to select all the pets of one type.

mysql_query("SELECT * FROM tablename WHERE type='cat'");Let's take a look now. WHERE adds a condition. So it will only select entries from table 'tablename' when one of the fields, in this case type, contains the work cat.You can add multiple requirements by placing an AND in between. for example,

mysql_query("SELECT * FROM tablename WHERE type='cat' AND owner='love'");In this case, the query will ONLY select data entries that satisfy both requirements. Now lets move on to another query type. Update is a way you can change what is in a specific field or fields of a data entry.

mysql_query("UPDATE tablename SET canadopt='yes'");In the above query, it's saying update table 'tablename' and set field 'canadopt' to yes for all entries. This is great if your trying to change something sweeping, but sometimes you only want to change one little thing. In that case you want to use something like this,

mysql_query("UPDATE tablename SET canadopt='yes' WHERE username='Chibi'");This query will only set the field 'canadopt' to yes when in the same data entry, the username is equal to 'Chibi'. One more thing we'll cover in the MySQL basics area is using variables in queries. Yes, the PHP variables you learned earlier. Here's an example,

mysql_query("SELECT * FROM tablename WHERE type='cat' AND owner='".$loggedinname."'");Alright so what do we notice here? You've probably noticed earlier that the actual thing your looking for in the data entry is in ' ' marks. So now within those ' ' marks, to put a variable we place the full quotation marks " ". Then within those marks, we use to periods. Within the periods you put the variable, starting with the $ sign. Personally I prefer this way because it pops out more in the coding and is colored differently in certain programs. You can also use this method to place variables in PHP strings, but we're not going there in this tutorial.


And there you have it. The basics of PHP and MySQL!

Hall of Famer
01-16-2011, 10:39 PM
Very helpful tutorials Kaeliah, I know you will make one. This should be good for aspiring PHP learners, lets hope we will have more PHP programmers in future.

*stickied*

Hall of Famer

Kaeliah
01-16-2011, 10:44 PM
Thanks Famer. :meow:

Hall of Famer
01-16-2011, 10:48 PM
You are very welcome. I will wait till you finish the loop tutorials and then post my tutorials for arrays.

Kaeliah
01-16-2011, 10:58 PM
Good :usedusedused: because I HATE :madO: arrays.

:happyc:

Hall of Famer
01-16-2011, 11:00 PM
lol...

Actually I dont see arrays or classes being used in RA, so guess they are not really required to work on modifications of this script. XD

Sasoragon
01-18-2011, 05:24 AM
...Kae, this is brilliant. :catfish:

This tutorial REALLY helps. I couldn't figure ANYTHING out about MySQL before now. (Mostly because w3school's MySQL tutorial is a strange-ish blur) :happycbig:

Kaeliah
01-18-2011, 08:44 AM
Yay I'm glad I could help. And ADORABLE avatar!!!

Hall of Famer
03-23-2011, 06:50 PM
Found a very interesting article about Mysql commands in PHP, looks like it will help a lot:
http://www.createafreewebsite.net/phpmysql/alter.html