Connection to database MySQL failed
i try to connect to MySQL DB via a PHP5 class but i didn't get it although my code is pretty correct, i make a class that contain function to establish connection and in other page i make instance and i call method that build connection but the connection is failed. here my class :
class ConnectionManipulationBaseDeDonnees {
private $bdd;
public function connection() {
try {
$pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION;
$bdd=new PDO('mysql:host=localhost;dbname=ssiphone','root','',$pdo_options);
}
catch(Exception $e) {
die('Erreur: '.$e->getMessage());
}
}
public function bdd() {
$this->connection();
return $this->bdd;
}
}
and in the other file my code for instantiation and invocation is :
include("../classes/ConnectionManipulationBaseDeDonnees.php开发者_如何学JAVA");
//on déclare une instance de connection de la classe
$cnx = new ConnectionManipulationBaseDeDonnees();
//une variable qui contient l`accées à la base
$bdd = $cnx->bdd();
if ($bdd) {
echo "connection succeeded";
} else {
echo "connection failed";
}
i got always the message "connection failed".
Inside the function connection()
in your class, change $bdd = new PDO(...)
to $this->bdd = new PDO(...)
精彩评论