To connect a database in PHP, you can use the PHP Data Objects (PDO) extension or the MySQLi extension. Both extensions provide an interface for connecting to and interacting with databases. Here's a basic example of connecting to a MySQL database using PDO:
PDO (Procedural Style):
- Using PDO in the procedural style involves a series of function calls.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name
// Establish connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// Close connection
mysqli_close($conn);
?>
PDO (Object-Oriented Style):
- Using PDO in the object-oriented style involves creating an instance of the PDO class.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name
try {
// Establish connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
MySQLi (Procedural Style):
- Using MySQLi in the procedural style involves a series of function calls.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name
// Establish connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// Close connection
mysqli_close($conn);
?>
MySQLi (Object-Oriented Style):
- Using MySQLi in the object-oriented style involves creating an instance of the mysqli class.
<?php
$servername = "localhost"; // replace with your database server name
$username = "your_username"; // replace with your database username
$password = "your_password"; // replace with your database password
$dbname = "your_database_name"; // replace with your database name
// Establish connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Close connection
$conn->close();
?>
Remember to replace the placeholders ($servername
, $username
, $password
, and $dbname
) with the actual values for your database in all four examples.
Choose the approach that best suits your coding style and requirements. Both PDO and MySQLi provide similar functionality for connecting to and interacting with databases in PHP, but PDO offers more database driver options and a consistent interface for working with different database systems.