9three Posted February 12, 2009 Share Posted February 12, 2009 Hey, What I'm trying to accomplish is add the database name within the first line to avoid having two lines. This is what I mean: $connection = new mysql('localhost', 'root', '', 'cms'); $query = "SELECT ID, Username, Password, Email FROM admins"; $connection->query($query); It's throwing me an exception saying that I did not select a database. I'm trying to avoid this: $connection = new mysql('localhost', 'root', ''); $connection->select(cms); $query = "SELECT ID, Username, Password, Email FROM admins"; $connection->query($query); Here is my class file: public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->connect(); } public function connect() { $this->link = mysql_connect($this->host, $this->username, $this->password); if (!is_resource($this->link)) Throw New Exception(mysql_error()); else return $this->link; } public function select() { $this->select = mysql_select_db($this->dbname, $this->link); if (!is_resource($this->select)) Throw New Exception(mysql_error()); else return $this->select; } I thought this was the way to do it but I guess I was wrong. Anyone know how to fix the parameters? Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/ Share on other sites More sharing options...
Brian W Posted February 12, 2009 Share Posted February 12, 2009 Does it work with that one line added? If so, dude, its one line! Add it and get on with it. Btw, you should only need to say it once per page, so we are only talking one line per page. No big deal. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760482 Share on other sites More sharing options...
gevans Posted February 12, 2009 Share Posted February 12, 2009 Why do you want to avoid the second line? You need it.. Unless you add another line to your class method selecting the database after a successful conection. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760484 Share on other sites More sharing options...
Brian W Posted February 12, 2009 Share Posted February 12, 2009 Try: public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->connect(); $this->select();//If I understand correctly (I don't use classes much yet), this will do it. } Sorry for being dramatic before. Now I see where you are going with it, I think. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760489 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Try: public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->connect(); $this->select();//If I understand correctly (I don't use classes much yet), this will do it. } Sorry for being dramatic before. Now I see where you are going with it, I think. Doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760492 Share on other sites More sharing options...
Brian W Posted February 12, 2009 Share Posted February 12, 2009 error message? doesn't select db but returns no error? whats going on? Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760500 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 Throws another Exception now within my constructor on the first line. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760504 Share on other sites More sharing options...
Brian W Posted February 12, 2009 Share Posted February 12, 2009 There is a hint of information online I am still researching but it suggests that you can not return objects in your constructor. If this is true, you will need to call both connect() and select() outside of new mysql() and your __construct() function Does connect() work from the __construct? Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760515 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 When you input a 'function' into the constructor it's called a method. And yes, $this->connect() works fine in the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760518 Share on other sites More sharing options...
gevans Posted February 12, 2009 Share Posted February 12, 2009 try this; public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->connect(); } public function connect() { $this->link = mysql_connect($this->host, $this->username, $this->password); if (!is_resource($this->link)) Throw New Exception(mysql_error()); else { $this->select(); return $this->link; } } public function select() { $this->select = mysql_select_db($this->dbname, $this->link); if (!is_resource($this->select)) Throw New Exception(mysql_error()); else return $this->select; } If it doesn't work post the error message Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760521 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 That puts me back to square one. Fatal error: Uncaught exception 'Exception' with message 'No database selected' in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php:40 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\gb\index.php(10): mysql->query('SELECT ID, User...') #1 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\gb\library\mysql.class.php on line 40 Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760537 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 Why are you putting the select in a different function if you are not even providing the option to add a different database as a parameter? <?php class mysql { private $host, $username, $password, $dbname; public function __construct($host = '', $username = '', $password = '', $dbname = '') { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->connect(); $this->select(); } public function connect() { $this->link = mysql_connect($this->host, $this->username, $this->password); if (!is_resource($this->link)) Throw New Exception(mysql_error()); return $this->link; } public function select($dbname='') { if (isset($dbname) && !empty($dbname)) { $this->dbname = $dbname; } $this->select = mysql_select_db($this->dbname, $this->link); if (!is_resource($this->select)) Throw New Exception(mysql_error()); else return $this->select; } ?> Now this code: $connection = new mysql('localhost', 'root', ''); $connection->select('cms'); // note quotes $query = "SELECT ID, Username, Password, Email FROM admins"; $connection->query($query); Should work. It should also work with the db being passed in as a parameter to the constructor. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760543 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 I am allowing the change of database in the parameters //host, username, password, database name public function __construct($host = '', $username = '', $password = '', $dbname = '') //host = localhost, username = root, password = blank, database = cms $connection = new mysql('localhost', 'root', '', 'cms'); Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760548 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 I am allowing the change of database in the parameters //host, username, password, database name public function __construct($host = '', $username = '', $password = '', $dbname = '') //host = localhost, username = root, password = blank, database = cms $connection = new mysql('localhost', 'root', '', 'cms'); Right, but you were using the $connection->select and trying to pass a parameter that way, which it does not accept a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760563 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 I don't want to use the object of $connection->select(); I want to avoid that. I'm trying to execute connecting and selecting the database when I first initiate the object. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760588 Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 I don't want to use the object of $connection->select(); I want to avoid that. I'm trying to execute connecting and selecting the database when I first initiate the object. In the revised code I posted that is done. I added functionality to the select(). But if you call connect() it does it automatically. However if you want to change the DB at any time you just call $connection->select('newdbhere'); Look over my revision to see the changes. $connection = new mysql('localhost', 'root', '', 'cms'); $query = "SELECT ID, Username, Password, Email FROM admins"; $connection->query($query); Should work, given the code I modified above. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760594 Share on other sites More sharing options...
9three Posted February 12, 2009 Author Share Posted February 12, 2009 hm.. I see what you did there. I remember seeing some code some time ago and it was handled differently. Thanks for the input. Quote Link to comment https://forums.phpfreaks.com/topic/144927-adding-database-name-within-the-initial-parameters/#findComment-760612 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.