Wife Posted April 24, 2017 Share Posted April 24, 2017 ok im trying to learn some PHP and i has problem if i change the db password to something else than it is it will give me an error, but it still wont connect with the right details pls help plox my connection class class connection { protected $db_connect; public $db_username = 'root'; public $db_hostname = 'localhost'; public $db_database = 'test'; public $db_password = ''; function db_connection() { try { $this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this->connection; } catch(PDOException $error) { echo $error->getMessage(); } } function debug_Connection() { if($this->db_connect == true) { echo 'Connection debugged.'; } } } $test = new connection; $test->db_connection(); pls help im stuck Quote Link to comment Share on other sites More sharing options...
Fratem Posted April 24, 2017 Share Posted April 24, 2017 Which localhosting service are you using? MAMP? Quote Link to comment Share on other sites More sharing options...
Fratem Posted April 24, 2017 Share Posted April 24, 2017 (edited) On 4/24/2017 at 11:18 AM, Facial said: ok im trying to learn some PHP and i has problem if i change the db password to something else than it is it will give me an error, but it still wont connect with the right details pls help plox my connection class class connection { protected $db_connect; public $db_username = 'root'; public $db_hostname = 'localhost'; public $db_database = 'test'; public $db_password = ''; function db_connection() { try { $this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $this->connection; } catch(PDOException $error) { echo $error->getMessage(); } } function debug_Connection() { if($this->db_connect == true) { echo 'Connection debugged.'; } } } $test = new connection; $test->db_connection(); pls help im stuck Expand Try changing class connection { protected $db_connect; public $db_username = 'root'; public $db_hostname = 'localhost'; public $db_database = 'test'; public $db_password = ''; to class connection { protected $db_connect; public $db_username = "root"; public $db_hostname = "localhost"; public $db_database = "test"; public $db_password = ""; Also, I usually work via DAO class DAO { private static $dbHost = "localhost"; private static $dbName = "kcart"; private static $dbUser = "kcartTest"; private static $dbPass = ""; private static $sharedPDO; protected $pdo; function __construct() { if(empty(self::$sharedPDO)) { self::$sharedPDO = new PDO("mysql:host=" . self::$dbHost . ";dbname=" . self::$dbName, self::$dbUser, self::$dbPass); self::$sharedPDO->exec("SET CHARACTER SET utf8"); self::$sharedPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$sharedPDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } $this->pdo =& self::$sharedPDO; } Edited April 24, 2017 by Howest Quote Link to comment Share on other sites More sharing options...
harrypotter Posted April 24, 2017 Share Posted April 24, 2017 (edited) If you're using MAMP you need to connect via socket, if not please give more info. Edited April 24, 2017 by harrypotter Quote Link to comment Share on other sites More sharing options...
harrypotter Posted April 24, 2017 Share Posted April 24, 2017 (edited) Also I believe this: $this->connection = new PDO("mysql:host=$this->db_hostname;$this->db_database", $this->db_username, $this->db_password); Is invalid, you need to specify that $this->db_datebase is the database name: $this->connection = new PDO("mysql:host=$this->db_hostname;dbname=$this->db_database", $this->db_username, $this->db_password); Might be wrong though Edited April 24, 2017 by harrypotter 2 Quote Link to comment Share on other sites More sharing options...
Explv Posted April 24, 2017 Share Posted April 24, 2017 Shouldn't you have dbname= before the database name in the dsn? 1 Quote Link to comment Share on other sites More sharing options...
Wife Posted April 24, 2017 Author Share Posted April 24, 2017 Yes, but it still wont print the debug_Connection() function.. I missed the dbname= in the PDO(), now it also gives me error IF the DB wont exists, but doesnt give me a connection succeed message when I try to print it. Quote Link to comment Share on other sites More sharing options...
harrypotter Posted April 24, 2017 Share Posted April 24, 2017 On 4/24/2017 at 11:45 AM, Facial said: Yes, but it still wont print the debug_Connection() function.. I missed the dbname= in the PDO(), now it also gives me error IF the DB wont exists, but doesnt give me a connection succeed message when I try to print it. Expand I'm not really sure what you mean, if should return an error if the database table doesn't exist. Why would it give a connection success message when it didn't successfully connect? If you're getting an error stating that the db table doesn't exist then double check your table name is correct Quote Link to comment Share on other sites More sharing options...
Wife Posted April 24, 2017 Author Share Posted April 24, 2017 On 4/24/2017 at 12:00 PM, harrypotter said: I'm not really sure what you mean, if should return an error if the database table doesn't exist. Why would it give a connection success message when it didn't successfully connect? If you're getting an error stating that the db table doesn't exist then double check your table name is correct Expand No, no and no. First I was unable to get the error even with false details, now it gives me the error I want when false details. But still wont print my debug function. Quote Link to comment Share on other sites More sharing options...
harrypotter Posted April 24, 2017 Share Posted April 24, 2017 On 4/24/2017 at 12:06 PM, Facial said: No, no and no. First I was unable to get the error even with false details, now it gives me the error I want when false details. But still wont print my debug function. Expand From the snippet you've shown you're not even calling the debug_Connection() function? Quote Link to comment Share on other sites More sharing options...