How do I check if my PHP + PDO code successfully connected to the MySQL database?
With the code it works:
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:host=localhost;port=3306;dbname=bancoservico';
$user = 'root';
$password = '';
$opcoes = array( PDO::ATTR_PERSISTENT => true,
PDO::ATTR_CASE => PDO::CASE_UPPER
);
try {
$dbh = new PDO($dsn, $user, $password, $opcoes);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The error occurs when the code looks like this:
<?php
class AcessoDados {
public function __construct(){
$dsn = 'mysql:host=localhost;port=3306;dbname=bancoservico';
$user = 'root';
$password = '';
$opcoes = array( PDO::ATTR_PERSISTENT => true,
PDO::ATTR_CASE => PDO::CASE_UPPER
);
try {
$dbh = new PDO($dsn, $user, $password, $opcoes);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
}
Link to test: link