9three Posted January 10, 2009 Share Posted January 10, 2009 Before adding my __construct my class was working fine, but I figured I would cut down some code by when I initiate the class, within the same parenthesis add the parameters as well. index.php $connection = new mysql('localhost', 'root', ''); $connection->select('cms'); Before I added my construct: $connection = new mysql(); $connection->connect('localhost', 'root', ''); $connection->select('cms'); Anyway, It's throwing me an exception: Fatal error: Uncaught exception 'Exception' with message 'Unable to select database' in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php:30 Stack trace: #0 C:\Users\9three\Desktop\Server\htdocs\CMS\admin\index.php(: mysql->select('cms') #1 {main} thrown in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php on line 30 As well as a MySQL error: Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Users\9three\Desktop\Server\htdocs\CMS\admin\includes\library\mysql.class.php on line 27 mysql.class.php public $host, $user, $password; public $link, $select, $query, $fetch; public function __construct($host, $user, $password) { $this->host = $host; $this->user = $user; $this->password = $password; } public function connect() { $this->link = mysql_connect($this->host, $this->user, $this->password); if (!$this->link) { Throw New Exception('Unable to connect'); } } public function select($select) { $this->select = mysql_select_db($select, $this->link); if (!$this->select) { Throw New Exception('Unable to select database'); } } my method select was working fine before... Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/ Share on other sites More sharing options...
corbin Posted January 10, 2009 Share Posted January 10, 2009 It needs to know if it's connected or not if you don't plan on telling it to connect. Something like (psuedo code, in the select function): if(not connected) { connect; } Or, you could just change the construct to: public function __construct($host, $user, $password) { $this->host = $host; $this->user = $user; $this->password = $password; $this->connect(); $this->select(); } Sometimes 'lazy' connections are done on purpose though. That way, the over head is smaller of an unused DB class. Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733820 Share on other sites More sharing options...
DarkWater Posted January 10, 2009 Share Posted January 10, 2009 It's all well and good that you're passing things into the constructor, but you're not actually connecting. Try changing __construct() to: public function __construct($host, $user, $password) { $this->host = $host; $this->user = $user; $this->password = $password; $this->connect(); } Also, I'd suggest making the variables private, not public. Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733821 Share on other sites More sharing options...
9three Posted January 10, 2009 Author Share Posted January 10, 2009 It needs to know if it's connected or not if you don't plan on telling it to connect. Something like (psuedo code, in the select function): if(not connected) { connect; } Or, you could just change the construct to: public function __construct($host, $user, $password) { $this->host = $host; $this->user = $user; $this->password = $password; $this->connect(); $this->select(); } Sometimes 'lazy' connections are done on purpose though. That way, the over head is smaller of an unused DB class. So are you saying that using $this->connect() in the constructor is the lazy way? Which way would be the not-lazy way? Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733828 Share on other sites More sharing options...
corbin Posted January 10, 2009 Share Posted January 10, 2009 Nooooo.... Lazily connection is a design term (sort of... more of a strategy or something). The first time I saw it was in Zend_Db, but I bet it's been around a long time. The concept of lazy connections basically is this: -Tell the DB instance the data it knows to connect, but do not have it connect. -When a query is run or something else requiring a database connection, then try to make the connection. The reasoning behind it is that if the DB class is never used, the DB connection is never made, therefore [as much] time isn't wasted. Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733833 Share on other sites More sharing options...
DarkWater Posted January 10, 2009 Share Posted January 10, 2009 No, lazy connections are connections that are initialized on-demand. Like, if you just instantiated a DB class, it wouldn't connect until you actually did your first operation, which could save some memory and resources. Depends on what you're actually trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733834 Share on other sites More sharing options...
9three Posted January 10, 2009 Author Share Posted January 10, 2009 Normally I only connect to a database when i HAVE to. So basically what you are saying is that if I do something like public function select($select) { $this->connect(); $this->query = mysql_select_db($select, $this->link); } It would actually be better? Quote Link to comment https://forums.phpfreaks.com/topic/140241-construct-giving-me-problems/#findComment-733843 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.