idire Posted November 18, 2008 Share Posted November 18, 2008 New to oo php, trying to work out mysql connections in classes. Here is the code: <?php class dbHandle { const HOST = 'localhost'; // Server const USERNAME = 'root'; // Username const PASSWORD = ''; // Password const DATABASE = 'db1'; // Database Name function connect() // Database Connection { $this->conn = mysql_connect(HOST, USERNAME, PASSWORD) or die("Database Connection Failure"); mysql_select_db(DATABASE); } } $test = new dbHandle(); $test->connect() ?> I get: Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'HOST' (11004) in class_lib.php on line 11 Have I defined something wrong? I'm not sure what to declare as $ variables in classes and what not to Link to comment https://forums.phpfreaks.com/topic/133193-solved-quick-connection-question/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2008 Share Posted November 18, 2008 http://us2.php.net/manual/en/language.oop5.constants.php self::HOST Link to comment https://forums.phpfreaks.com/topic/133193-solved-quick-connection-question/#findComment-692728 Share on other sites More sharing options...
idire Posted November 18, 2008 Author Share Posted November 18, 2008 Much appreciated this code works: <?php $this->conn = mysql_connect(self::HOST, self::USERNAME, self::PASSWORD) or die("Database Connection Failure"); ?> Link to comment https://forums.phpfreaks.com/topic/133193-solved-quick-connection-question/#findComment-692730 Share on other sites More sharing options...
corbin Posted November 18, 2008 Share Posted November 18, 2008 Why would database parameters be constants? Would you expect those to never change? Link to comment https://forums.phpfreaks.com/topic/133193-solved-quick-connection-question/#findComment-693042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.