PHP MySQL Tutorial

PHP and MySQL are used together to build most of the dynamic websites on the internet. PHP is the language that runs on the server, and MySQL is the database where the data is stored. WordPress is a very popular example of this combination.

In this tutorial you will learn how to connect PHP with a MySQL database and how to create, read, update and delete data (called CRUD operations). All chapters are written for beginners, with simple examples.


What you will learn


What you need before you start

  • Basic knowledge of PHP, especially variables, functions and arrays.
  • A local server like XAMPP, WAMP or MAMP. It gives you PHP, Apache and MySQL in one package.
  • phpMyAdmin (comes with XAMPP) to see and manage your database in the browser.

MySQLi or PDO?

PHP has two main ways to talk to MySQL: MySQLi (MySQL Improved) and PDO (PHP Data Objects). In this tutorial we use MySQLi, because it is simple for learning. You can read the difference in mysqli vs PDO and the two coding styles in MySQLi Procedural vs Object Oriented.

Note: The old mysql_* functions (without the “i”) were removed in PHP 7. If you see them in an old tutorial, do not use them.

Example used in these chapters

All the chapters use the same example. We create a database named student and a table named users with the columns id, name, mobile and email. The connection code is saved in one file, db-connection.php, so you can reuse it everywhere.

Click on the next chapter to start learning.


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Display MySQL data in an HTML table using mysqli_fetch_array()
Interview Questions & Answers
What is the difference between mysqli_fetch_array() and mysqli_fetch_object()?
How to Fix Incorrect format parameter error when importing large database in PHPMyAdmin?
In how many ways can we fetch data from a MySQL result set in PHP?
How to encrypt and decrypt data stored in a MySQL table?
What is the difference between mysqli and PDO in PHP?