
In this tutorial I will teach you the basics of PHP and it's involvement with MySQL databases to which you can incorporate to many types of web development projects. This provides an absolute beginner-entry instruction to begin programming in this manner.
It is assumed that you either have access to a server with php installed and access to MySQL databases (almost every host provides this) or have installed both on your computer. Most hosts also provide phpmysqladmin so I'm not going to teach you the manual way of creating databases and adding tables. There are plenty of tutorials if you do not already know how to do this and don't have phpmysqladmin.
1) Basic PHP and connecting to your database
Note: code lines starting with // are called comments and are skipped over when the file is executed. These allow me to make notes about what each line is doing.
Making a PHP file
All php files must end in .php. Create a file with this extension (I'm going to use test.php). In every php file you must indicate that php code is being used by the tags <?php and ?>
PHP Code:
<?php // indicates the start of the php code
// everything you want executed goes in here.
//indicates the end of the php code
?>
Basic PHP Commands
The semicolon (;) indicates the end of a line. Omitting them is a common error.
Echo command - outputs a string of text; syntax: echo "TEXT HERE";
Variables - a variable holds a string of information for use later. It is very important in almost every usage of php. The $ is used to indicate a variable. The syntax to create a variable is: $NameOfVariable = "Text string to be saved";
Once information is inside a variable you can do various things with it such as math operations.
PHP Code:
<?php
echo "Hello, I am about to create a variable";
$MyVariable = "My favorite number is:"; //Creates a variable with a string of text in it
$MyOtherVar = 5; //Creates a variable with the number 5 in it.
$MyFinalVar = $MyOtherVar * 8; //Makes a third variable that multiplies 8 times 5
echo $MyVariable;
echo $MyFinalVar;
// This output "My favorite number is: 40"
?>
Connecting to the Database
Once you have set up your database, please note the name of your database and the username and password of the user attached to it. Server is localhost unless your MySQL is at a separate location in which case you will need to put that IP address.
To connect to the database you will use the following commands:
mysql_connect(SERVER LOCATION, USERNAME, PASSWORD);
mysql_select_db("DB NAME");
After these mysql commands we can add: or die() in order to get an error notifaction if something is wrong. This command can be used with a custom error message or with an automated message: die(mysql_error());
PHP Code:
<?php
mysql_connect(localhost, user1, pass1) or die("Connection failed");
mysql_select_db("test") or die ("Database selection failed");
?>
END OF PART 1
2) Communication with Database
Now that you know how to connect to your database, let's use it!
Insert data into your database
This inserts new data into a database table. You must make sure to check the name of your table and the names of the individual fields within your table. If you are unsure how to properly set up your table, please look for information about this.
We will use this command:
mysql_query("INSERT INTO table_name (field1, field2) VALUES ('value1', 'value2') ");
Note: In the value spots you do not need quotes if you are using a variable.
PHP Code:
<?php
mysql_connect(localhost, user1, pass1) or die("Connection failed"); //connect
mysql_select_db("test") or die ("Database selection failed"); //select db
$value1 = "Hello";
$value2 = "See ya";
mysql_query("INSERT INTO phrases (greetings, goodbyes) VALUES ($value1, $value2) ") or die(mysql_error());
// This inserts the data "Hello" into the greetings field of the phrases table
// and "See ya" into the goodbyes field. The values are put into variables
// and therefore quotes are not needed in the query.
?>
Update and delete date already in database
This is the syntax of our queries:
mysql_query("UPDATE table_name SET field='value' WHERE field='previous_value'");
mysql_query("DELETE FROM table_name WHERE field='value'");
PHP Code:
<?php
mysql_connect(localhost, user1, pass1) or die("Connection failed"); //connect
mysql_select_db("test") or die ("Database selection failed"); //select db
mysql_query("UPDATE phrases SET greetings='Hi' WHERE greetings='Hello'") or die(mysql_error());
// This updates the greetings entry for "Hello" and replaces it with "Hi"
mysql_query("DELETE FROM phrases WHERE goodbyes='See ya'") or die(mysql_error());
// This deletes the goodbyes entry of "See ya" from the phrases table
?>
This is the end of the tutorial for now but it will be expanded if it is well received. It was written by me not copy and pasted from somebody else. Please press the THANKS button if you found this helpful.