PHP Connect to MySQL
To connect PHP to MySQL, you can use the mysqli extension or PDO (PHP Data Objects). Here's an example of how to connect using mysqli:
Should I Use MySQLi or PDO?
Both MySQLi and PDO have their advantages:
Advantages of MySQLi:
- Specifically Designed for MySQL: MySQLi is designed specifically for MySQL, which means it offers some MySQL-specific features that PDO may not have.
- Procedural and Object-Oriented Interface: MySQLi provides both procedural and object-oriented interfaces, giving developers flexibility in coding style.
- Supports Asynchronous Queries: MySQLi supports asynchronous queries, allowing for non-blocking database queries which can be useful for certain types of applications, like real-time systems.
- Improved Security Features: MySQLi offers enhanced security features such as support for prepared statements and parameterized queries, helping prevent SQL injection attacks when used correctly.
Advantages of PDO:
- Database Portability: PDO provides a uniform interface for accessing databases, which means you can easily switch between different database systems (MySQL, PostgreSQL, SQLite, etc.) with minimal code changes.
- Support for Multiple Databases: In addition to MySQL, PDO supports a wide range of database systems, making it a good choice for projects that may need to work with different databases.
- Object-Oriented Interface: PDO exclusively offers an object-oriented interface, which may be preferred by developers who favor object-oriented programming practices.
- Prepared Statements and Parameterized Queries: PDO supports prepared statements and parameterized queries, just like MySQLi, providing a secure way to interact with databases and helping prevent SQL injection attacks.
Example
<!DOCTYPE html> <html> <body> <?php $servername = "localhost"; $username = "username"; $password = "password"; $database = "database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>You can click on above box to edit the code and run again.
Output
Open a Connection to MySQL
Before we can access data in the MySQL database, we need to be able to connect to the server:
Example (MySQLi Object-Oriented)
<!DOCTYPE html> <html> <body> <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>You can click on above box to edit the code and run again.
Output
Example (MySQLi Procedural)
<!DOCTYPE html> <html> <body> <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>You can click on above box to edit the code and run again.
Output
Example (PDO)
<!DOCTYPE html> <html> <body> <?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>You can click on above box to edit the code and run again.