Learn PHP and MySQL

PHP is one of the most widely-used programming languages on the internet, and it allows you to do much more than simple HTML. MySQL allows you to easily create and modify databases on your server. Used together, these tools can create complex and powerful custom websites and databases. There’s a lot to learn when it comes to PHP and MySQL, but getting up and running with the basics will only take a little bit of time. See Step 1 below to get started.


Getting Prepared

  1. Understand what PHP and MySQL are. PHP is a scripting language that you can use to build interactive scripts. These scripts are executed on the web server and then the results are returned to the viewer via HTML. PHP allows for very interactive and user-focused websites. MySQL is an open-source database language. It allows you to create, edit, and access multiple databases on your server. The combination of these two is essential for online stores, forums, games, and more.
    • PHP can collect form information from the user, create and edit files on the server, send and receive cookies, restrict access, encrypt data, and much more.
  2. Rent or create a web server. In order to use PHP and MySQL, you will need access to a web server. If you don’t have access to a web server, you will need to install one on your own computer.
  3. Find some resources. There are a variety of ways that you can learn the ins and outs of PHP and MySQL coding. There are online resources, online courses, books, and classroom courses. All of these can help you learn PHP and MySQL.
    • The most popular online resource is w3schools.com. This is the premiere web development instruction website, and quickly covers the basics along with interactive tutorials.
    • There are a variety of books available as well. Some of the most popular books include “Learning PHP, MySQL, JavaScript, & CSS” by Robert Nixon and “PHP and MySQL Web Development” by Luke Welling.
    • Check the course listings for your local community college. There may also be programming schools in your area, or classes led in community centers. Hands-on classes led by professionals can be great for getting questions answered and seeing the code in action.
  4. Download the necessary tools. To begin creating PHP scripts and MySQL databases, you will need to download a few basic tools. While PHP can be edited in any text editor, you will find that using a dedicated coding editor will make your life a lot easier.
    • Popular free editors include Notepad++, Komodo Edit, NetBeans, and Eclipse.
    • Popular paid programs include PhpStorm, Adobe Dreamweaver, and PHP Designer.
    • In order to use MySQL, you will need to have it Install-the-MySQL-Database-Server-on-Your-Windows-PC.

Creating Basic PHP Scripts

  1. Open your text editor. PHP is created in any text editor, though a dedicated coding editor will highlight your syntax and make it much easier to read.
  2. Create a basic website. PHP exists within a standard HTML file. In order to see the results of your PHP script, you will need to have a basic website to display it:
  3. Create a basic ECHO script. The “ECHO” function will print back the text to the website. This is a basic function of PHP, and will help you learn how to format PHP syntax. All PHP scripts start with <?php and end with ?>. Statements are ended with a semicolon (;).
  4. Add comments to your PHP script. This is a good practice to get into. Comments aren’t displayed to the user, but they allow other developers to see what you are doing. They can also help you remember what you were attempting if you revisit the code later. [1]
  5. Create a script with some basic variables. Variables are objects that have values assigned to them in the script. You can then manipulate these variables to display results to the user. Variables are a powerful part of PHP scripting, and are denoted by a “$” before the variable.[2]
  6. Create a basic If/Else statement. A lot of the functionality of PHP comes from the If/Else statements. These allow you to create conditions for specific commands to occur. This is especially useful for creating customized messages, as well as checking connections.[3]

Creating a Basic MySQL Database

  1. Connect to a MySQL server. You will need to connect to the MySQL server before you can create your database. You can do this through the MySQL command line interface, or by using PHP, which is what will be covered here. Creating a connection uses the mysqli_connect(host, username, password) function.[4]
    • The connection to the database is assigned to the variable “$connection”. This will allow you to easily refer to the connection later in the script.
    • To see how to create a database using the MySQL command line, check out this article.
  2. Create your database. Once you’ve opened the connection, you can add in code to create the database. The database will not contain any data yet; the first table in the database will be added in the next step. You will be using the CREATE DATABASE statement to create the database.
  3. Create a table for your database. Once the database has been created, you can create a table to store data that you retrieve from forms. The table can be configured in any way you’d like to fit your data. The table created in this step will have three columns: FirstName, LastName, and Age. The table will be called “Users”

Creating a Form to Enter Data into Your Database

  1. Create your HTML form. This form will allow users to enter their information into a form on the website. This data will then be stored into a file and inserted into the database you created earlier. When the user clicks the Submit button after filling out the form, the data will be sent to the “insert.php” file.[5]
  2. Create the insert.php file. Once you have the form created, you will need to create the insert.php file to handle transferring the data to your database. You will be using the INSERT INTO statement to add the record to your Users table.

Continuing to Learn

  1. Learn what can be done with PHP. Beyond database management, there is a lot that can be accomplished with PHP. You can open files, send emails, create cookies, start private sessions, and much more. The potential is nearly limitless, which is why so much web development is done with PHP.
  2. Examine what others do. One of the quickest ways to learn PHP is to examine the code that other developers create and adapt it to your won. While there is no way to examine the PHP code of a website without direct access to the server that it is hosted on[6], there are a variety of communities that share code and can help explain what is going on in it.
    • GitHub is one of the more popular repositories for open source code and collaboration[7]
  3. Learn PHP security. Security on the web is a serious concern, and making sure that your code is secure is essential. This is especially important if you are dealing with passwords and payment information. Make sure that your forms and databases are secure from any intrusions.
    • See Create-a-Secure-Login-Script-in-PHP-and-MySQL for a detailed look at creating a secure login with PHP and MySQL.

Related Articles

  • Create-a-Secure-Login-Script-in-PHP-and-MySQL
  • Open-a-PHP-File
  • Write-PHP-Scripts
  • Create-and-Call-PHP-Functions
  • Learn Web Design

Sources and Citations