macross Posted March 1, 2011 Share Posted March 1, 2011 So i am currently coding database connection class and i have encountered very strange behavior from my script. base.class.php: <?php class base{ private $settings; function get_settings(){ $settings["dbhost"] = 'localhost'; $settings["dbuser"] = '*****'; $settings["dbpass"] = '*****'; $settings["dbname"] = 'core'; return $settings; } } ?> database.class.php <?php require_once 'base.class.php'; class database extends base{ private $query_now; private $link; public function __construct(){ $settings = base::get_settings(); $dbhost = $settings["dbhost"]; $dbuser = $settings["dbuser"]; $dbpass = $settings["dbpass"]; $dbname = $settings["dbname"]; $this->link = mysql_connect($dbhost, $dbname, $dbpass) or die ("Could not connect to the mysql database"); mysql_select_db($dbname, $this->link) or die ("Could not select the database"); } function query($query){ $this->query_now = $query; return mysql_query($query, $this->link); } function getArray($result){ return mysql_fetch_array($result); } } ?> When i try to create an instance of database class, i get mysql_connect error. I have tried to echo my array and it seems that correct information is being passed over. Now the strange thing is if i remove my password from the base class i don't get a mysql_connect error but this time instead i get "Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'core'@'localhost' (using password: YES) " In case you are wondering, does my mysql database user has a password, the answer is: yes for sure... (Also i have tried to setup a simple script for connecting to my database and everything worked fine) So any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/229225-strange-behavior/ Share on other sites More sharing options...
trq Posted March 1, 2011 Share Posted March 1, 2011 get_settings() is not a static method, why are you calling it as if it where? Quote Link to comment https://forums.phpfreaks.com/topic/229225-strange-behavior/#findComment-1181119 Share on other sites More sharing options...
macross Posted March 1, 2011 Author Share Posted March 1, 2011 So the correct way of calling a non static method would be $settings = $this->get_settings();? Because i get same error if i where using base::get_settings() or $this->get_settings(); Quote Link to comment https://forums.phpfreaks.com/topic/229225-strange-behavior/#findComment-1181131 Share on other sites More sharing options...
macross Posted March 1, 2011 Author Share Posted March 1, 2011 Finally found out what was wrong so problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/229225-strange-behavior/#findComment-1181156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.