2011-03-04 142 views
0

我尝试通过PHP5类连接到MySQL数据库,但我没有得到它,虽然我的代码是非常正确的,我做了一个包含函数来建立连接的类,并在其他页面我做实例我打电话建立连接但连接失败的方法。 这里我的课:连接数据库MySQL失败

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; 
    } 
} 

,并在其他文件我的代码实例化和调用是:

include("../classes/ConnectionManipulationBaseDeDonnees.php"); 

//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"; 
} 

我总是得到消息,“连接失败”。

回答

0

你们班function connection()内部,改变$bdd = new PDO(...)$this->bdd = new PDO(...)

+0

THX了很多,它的工作:) – Malloc 2011-03-04 20:10:51

+0

欢迎您:) – 2011-03-04 20:23:49