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
- PHP : MySQL Database: what MySQL is and how data is stored in tables.
- PHP : MySQL Connect: connect your PHP script to the database.
- PHP : MySQL Create Database and Create Table: build the structure.
- INSERT: add new records.
- SELECT: read records from the table.
- UPDATE: change existing records.
- DELETE: remove records.
- Prepared Statements: run queries safely and stop SQL injection.
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.
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.
| Exercises & Assignments |
|---|
| Display MySQL data in an HTML table using mysqli_fetch_array() |