Make sure that /etc/php/x.x/cli/php.ini contains the following parameter to tell PHP to load PDO. This example is for MySQL.
extension=pdo.so
Set a new data source name variable - I'll be using MySQL
$dsn="mysql:host=$dbHost;dbname=$dbName;charset=$dbCharset";
Create an array that contains desired DB options
$dbOptions = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
Use a try statement to create a new database connection that will return any errors upon failure
try {
$pdo = new PDO($dsn, $dbUsername, $dbPassword, $dbOptions);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
echo "error";
}
Setup a prepared statement to prepare your query for execution
$guestCount=$pdo->prepare("SELECT names FROM guestList WHERE age > :age");
Set any variables that were in the prepared statement and execute
$guestCount->execute(['age'=>$guestAge]);
Close the connection
$pdo=null;