PHP MySQL Introduction
Most modern websites aren't just static HTML pages; they are dynamic, data-driven applications. Whether it's a blog, an e-commerce site, or a social network, they all need a way to store and retrieve data. This is where **MySQL** comes in.
What is MySQL?
MySQL is the world's most popular open-source relational database management system (RDBMS). It uses **Structured Query Language (SQL)** to manage and manipulate data stored in tables.
- Relational: Data is organized into tables that can be linked to each other.
- Server-side: MySQL runs on a server and waits for requests (queries).
- Fast & Scalable: It can handle everything from tiny personal blogs to massive enterprise databases.
- Free: It is open-source and free to use for most projects.
PHP + MySQL: The Perfect Match
PHP and MySQL were practically made for each other. PHP provides the logic to process user requests, while MySQL provides the storage to keep the data safe. Together, they form the "Back-end" of millions of websites.
How it works:
- A user fills out a form in their browser.
- The browser sends the data to a PHP script on the server.
- The PHP script connects to the MySQL database.
- PHP sends a command (SQL query) to save or get data.
- MySQL responds, and PHP builds an HTML page to show the result to the user.
Two Ways to Connect
PHP offers two main ways to communicate with a MySQL database. Both are great, but they serve slightly different needs:
| Feature |
MySQLi (Improved) |
PDO (PHP Data Objects) |
| Database Support |
Only MySQL. |
Supports 12 different database systems (PostgreSQL, Oracle, etc.). |
| Style |
Procedural and Object-Oriented. |
Only Object-Oriented. |
| Portability |
Tied to MySQL. |
Easy to switch database types later. |
Recommendation: If you are building a small project that will only ever use MySQL, MySQLi is fine. For larger, more professional applications, PDO is usually preferred because of its flexibility.
Key Terminology
- Database: A container that holds tables and other data structures.
- Table: A collection of related data entries (like an Excel sheet).
- Record (Row): A single entry in a table (e.g., one user's info).
- Field (Column): A specific piece of information in a record (e.g., "Email" or "Password").
- Query: A command sent to the database (e.g., "SELECT all users").
Tip: You don't need to be an SQL expert to start using PHP with MySQL, but learning basic SQL commands like SELECT, INSERT, and UPDATE will make your life much easier.
Summary
- MySQL is a database used to store website data permanently.
- PHP acts as the bridge between the user and the database.
- You can use either MySQLi or PDO to connect.
- Data is stored in Tables consisting of Rows and Columns.