Mysql Connect in PHP
To connect to a MySQL database using PHP, you typically use either the MySQLi or PDO (PHP Data Objects) extension. Here, I'll explain both methods.
1. MySQLi (MySQL Improved)
a. Connecting to MySQL with MySQLi (Procedural Style):
<?php// Database connection parameters$host = "localhost"; // Host name (usually localhost)$username = "root"; // Database username$password = ""; // Database password$dbname = "test"; // Database name// Create connection$conn = mysqli_connect($host, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}echo "Connected successfully";// Close connectionmysqli_close($conn);?>b. Connecting to MySQL with MySQLi (Object-Oriented Style):
<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "test";// Create connection$conn = new mysqli($host, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}echo "Connected successfully";// Close connection$conn->close();?>2. PDO (PHP Data Objects)
PDO provides a more flexible and secure way to interact with databases. It supports multiple database systems, including MySQL, PostgreSQL, SQLite, etc. PDO also supports prepared statements, making it more secure and preventing SQL injection.
a. Connecting to MySQL with PDO:
<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "test";// DSN (Data Source Name)$dsn = "mysql:host=$host;dbname=$dbname";// Set options (optional)$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Throw exceptions on errors PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Fetch results as associative arrays PDO::ATTR_EMULATE_PREPARES => false // Use native prepared statements];try { // Create a PDO instance $pdo = new PDO($dsn, $username, $password, $options); echo "Connected successfully"; // If connection is successful} catch (PDOException $e) { // Handle connection error echo "Connection failed: " . $e->getMessage();}?>Differences Between MySQLi and PDO:
| Feature | MySQLi | PDO |
|---|---|---|
| Supported Databases | MySQL only | Multiple databases (MySQL, PostgreSQL, SQLite, etc.) |
| Connection Method | Procedural and Object-Oriented | Object-Oriented only |
| Prepared Statements | Yes | Yes |
| Error Handling | Uses mysqli_error() or exceptions | Uses exceptions by default |
| Fetch Modes | Associative arrays, Numeric arrays, etc. | Multiple fetch modes (e.g., PDO::FETCH_ASSOC) |
| Transaction Support | Yes | Yes |
Which to Use?
MySQLi is specific to MySQL and provides both procedural and object-oriented interfaces.
PDO is more flexible as it can work with different types of databases, and it has better security features like prepared statements.
Summary:
To connect to a MySQL database in PHP, you can use either MySQLi (MySQL Improved) or PDO (PHP Data Objects).
MySQLi is MySQL-specific and can be used in procedural or object-oriented styles.
PDO is more versatile, supporting various databases and offering better security and flexibility.
Let me know if you'd like more details or examples on any of these!