Wife Posted April 24, 2017 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
Fratem Posted April 24, 2017 Posted April 24, 2017 (edited) 8 minutes ago, 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 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
harrypotter Posted April 24, 2017 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
harrypotter Posted April 24, 2017 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
Explv Posted April 24, 2017 Posted April 24, 2017 Shouldn't you have dbname= before the database name in the dsn? 1
Wife Posted April 24, 2017 Author 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.
harrypotter Posted April 24, 2017 Posted April 24, 2017 13 minutes ago, 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. 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
Wife Posted April 24, 2017 Author Posted April 24, 2017 6 minutes ago, 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 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.
harrypotter Posted April 24, 2017 Posted April 24, 2017 3 minutes ago, 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. From the snippet you've shown you're not even calling the debug_Connection() function?