php not connecting to mysql database MAMP -


so have php connect database i'am running using mamp not working. class.database.php (sorry code bit messy). guy think problem or else in code? help.

    <? class dbconnection {         protected $db_conn;         public $db_name = 'todo';         public $db_user = 'root';         public $db_pass = 'root';         public $db_host = 'localhost';          function connect(){         try{             $this->db_conn = new pdo("mysql:host=$this->db_host;db_name=$this",$this->db_user,$this->db_pass)                     return $this->db_conn;     }                 catch(pdoexception $e)                 {                     return $e->getmessage();                 }         }             }  ?> 

you need replace

db_name=$this 

with

db_name=$this->db_name 

also, need place semi-colon @ end of line.

with mistakes, should getting php , pdo errors. check php logs errors. can run script command line using "php myscript.php" see errors (though you'll need invoke class). display pdo errors, add line below "new pdo" line:

$this->db_conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -