There are more ways to connect to a MySQL database in PHP. PDO (PHP Data Objects) is the best of them.
Advantages of PDO is a consistent interface across many different types of database, uses an object-oriented approach, and supports more features offered by newer databases.

Using a PDO database connection recommends wherever possible.


An example connection a MySQL database called a "test" with PDO

<?php
$servername 
"localhost";
$username "root";
$password "";

try {
    
$conn = new PDO("mysql:host=$servername; dbname=test"$username$password);
    
// set the PDO error mode to exception
    
$conn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    echo 
"Database TEST successfully connected"
    }
catch(
PDOException $e)
    {
    echo 
"Connection failed: " $e->getMessage();
    }
?>






Privacy Policy