CHAPTER 27
Beginner
PHP to MySQL Database Connection | mysqli vs PDO Tutorial
Updated: May 16, 2026
15 min read
# CHAPTER 27
Connecting SQL Databases with PHP
1. Introduction
You have spent 26 chapters mastering SQL within a graphical interface like phpMyAdmin. But end-users don't use phpMyAdmin; they use web browsers. To bridge the gap between a user clicking a button on a website and the database executing an SQL query, we need a backend programming language. In this chapter, we will use PHP, the language that powers nearly 80% of the web, to establish a secure connection to our SQL database and execute queries programmatically.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the role of PHP as the intermediary layer.
-
Differentiate between the
mysqliandPDOextensions.
- Establish a database connection using credentials.
- Handle connection errors securely.
- Execute a basic SQL query from a PHP script.
3. The Architecture (Browser -> PHP -> SQL)
-
1.
The User visits
index.phpin their browser.
- 2. The PHP script boots up on the server.
- 3. The PHP script securely logs into the SQL database using a username and password.
-
4.
The PHP script sends a text string containing SQL (e.g.,
"SELECT * FROM users") to the database.
- 5. The database processes the SQL and hands the data back to PHP.
- 6. PHP converts the data into HTML and sends it to the user's browser.
4. mysqli vs PDO
PHP has two primary ways to connect to a database:
-
mysqli(MySQL Improved): Designed specifically and exclusively for MySQL databases. It is slightly faster but limits you to one database engine.
-
PDO(PHP Data Objects): The modern, universal standard. PDO can connect to MySQL, PostgreSQL, SQLite, and Microsoft SQL Server using the exact same PHP code.
*We will cover both, but PDO is the industry standard for modern enterprise applications.*
5. Connecting with mysqli (Procedural)
To connect, you need 4 pieces of information: Host, Username, Password, and Database Name.
php
6. Connecting with PDO (Object-Oriented)
PDO uses a "Data Source Name" (DSN) string and try/catch blocks for robust error handling.
php
7. Executing a Simple Query (PDO)
Once connected, you can send SQL commands directly to the database.
php
8. Common Mistakes
-
Hardcoding Credentials in Public Files: Beginners often put their database password directly inside
index.php. If the PHP server crashes, the raw code might be displayed to the user, exposing the password! Passwords should always be stored in a separate, hidden.envfile that PHP reads securely.
-
Suppressing Errors: Using
@mysqliconnect()suppresses error messages. If the connection fails, you will get a blank white screen and have no idea why. Always handle connection errors properly!
9. Best Practices
- Standardize on PDO: Unless you are maintaining a 15-year-old legacy codebase, you should always choose PDO. It supports Prepared Statements (Chapter 26) perfectly and allows you to switch from MySQL to PostgreSQL in the future by changing exactly one line of code (the DSN).
10. Exercises
- 1. What are the four specific credentials required to authenticate a connection to an SQL database?
- 2. What is the modern, universal PHP extension used to connect to almost any relational database engine?
11. SQL Challenges
Write the exact PHP variable$dsn string required for a PDO connection to a MySQL database running on localhost with the database name ecommercedb and charset utf8mb4.
php
12. MCQ Quiz with Answers
Question 1
What is the primary architectural advantage of using the PDO extension over the mysqli extension when connecting a PHP backend to a database?
Question 2
When establishing a database connection in PHP, what is the purpose of the try/catch block?
13. Interview Questions
- Q: Explain the role of the backend server (PHP/Node.js) in a modern web architecture. Why doesn't the user's browser (JavaScript) simply connect directly to the SQL database? (Hint: Security and Credential exposure).
-
Q: Compare and contrast the
mysqliandPDOextensions. Which would you choose for a new enterprise project and why?
14. FAQs
Q: I get "Access denied for user 'root'@'localhost'". Why? A: This means your connection code works perfectly, but you provided the wrong password or username! If you are using a standard XAMPP installation, the username is usuallyroot and the password is an empty string "".
15. Summary
You have built the bridge. By defining your credentials, selecting the modern PDO extension, and handling connection exceptions gracefully, you have successfully linked your web application to your secure database vault, allowing the two systems to communicate seamlessly.16. Next Chapter Recommendation
Our PHP script can connect to the database and run a simpleSELECT query. But how do we build a real application? How do we securely INSERT data that a user types into a form? In Chapter 28: Building CRUD Applications with SQL, we will utilize Prepared Statements to build a complete, interactive web system.