sngskunk Posted June 7, 2006 Share Posted June 7, 2006 Im trying to make a class to connect to my mysql database but for some reason its not working cand you tell me what i need to do.[code]<?phpclass DB_Connect { var $server; var $username; var $password; var $database; function db_connect() { $dbserver = $this->server; $dbuser = $this->username; $dbpass = $this->password; $db = $this->database; $connect = mysql_connect($dbserver, $dbuser, $dbpass); mysql_select_db($db, $connect); }}$db = new DB_Connect;$db->server = "localhost";$db->username = "user";$db->password = "pass";$db->database = "database";$db->db_connect();?>[/code]I get this error:Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files (x86)\xampp\htdocs\portal\new\test.php on line 17Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Program Files (x86)\xampp\htdocs\portal\new\test.php on line 18But the server, username, password, and database are correct. Help??? Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/ Share on other sites More sharing options...
nogray Posted June 8, 2006 Share Posted June 8, 2006 I think you need a constructor function that will assign the values to your class variables.The constructor function in php 4 must be the same name as the class "function DB_Connect()"in that function assign the values of the $server....... Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43012 Share on other sites More sharing options...
High_-_Tek Posted June 8, 2006 Share Posted June 8, 2006 Or you can use: ___constructor() Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43017 Share on other sites More sharing options...
poirot Posted June 8, 2006 Share Posted June 8, 2006 Although the code could be better, it should work. Constructors are not needed, but they may be useful.Actually it's the constructor that is causing the problem.The constructor is called as soon as the class is initiated, not allowing you to set the server, username, etc.Try to rename the method to "connect" and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43024 Share on other sites More sharing options...
trq Posted June 8, 2006 Share Posted June 8, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Actually it's the constructor that is causing the problem.[/quote]There is no constructor in the code posted, php is case sensitive.The fact is this class is working fine. You seem to have a permissions problem. Are you sure the username and password are correct? Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43050 Share on other sites More sharing options...
poirot Posted June 8, 2006 Share Posted June 8, 2006 Oddly enough, it works here as constructor.OS related maybe? Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43051 Share on other sites More sharing options...
sngskunk Posted June 8, 2006 Author Share Posted June 8, 2006 Okay i changed $server to $address and db_connect() to connect().What would be the best way to write this poirot (any help is great).Thanks for the help!!!!!!!!!!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/11436-class-for-connecting-to-mysql/#findComment-43057 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.