Sam Granger Posted January 19, 2008 Share Posted January 19, 2008 Hello PHP gurus! First of all, thanks for having a look at my code. I'm quite a newbie with PHP(5). I tried making this class but I'm getting an error: Fatal error: Call to private DBConnection::__construct() from invalid context in /home/php5/public_html/mysql/index.php on line 11 My MySQL Class: <?php /* @author Sam Granger @copyright 2008 */ class DBConnection { private function __construct() { $_link = mysql_pconnect($db_server, $db_username, $db_password, $db_database); } function __destruct() { @mysql_close(); } public function DBRow() { $row = mysql_fetch_row($query); return $row; } public function DBArray() { $array = mysql_fetch_array($query); return $array; } public function DBCount() { $num = mysql_num_rows($query); return $num; } public function DBQuery() { $query = mysql_query($sql) or die(mysql_error()); return $query; } } ?> My index.php: <?php /* @author Sam Granger @copyright 2008 */ include('configuration.php'); include('mysql.class.php'); $DB = new DBConnection; $sql = 'SELECT title FROM news'; $query = $DB->Query($sql); while($array = $DB->DBArray($query)) { extract($array); echo '<div>$title</div>'; } ?> What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/ Share on other sites More sharing options...
Barand Posted January 19, 2008 Share Posted January 19, 2008 The immediately obvious errors are You dont pass ($db_server, $db_username, $db_password, $db_database) to your __Construct method so they are undefined. You have no class variables eg $link, $query so these are undefined in any methods using them. You have a DBQuery() method but you call $DB->Query() Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443691 Share on other sites More sharing options...
Sam Granger Posted January 19, 2008 Author Share Posted January 19, 2008 Ok, I have this now. Still same error however: Mysql: <?php /* @author Sam Granger @copyright 2008 Intrellia. All Rights Reserved. */ class DBConnection { var $connection; var $query; private function __construct($db_server, $db_username, $db_password, $db_database) { $this->connection = mysql_pconnect('$db_server', '$db_username', '$db_password'); mysql_select_db('$db_database', $this->connection); } function __destruct() { @mysql_close($this->connection); } public function DBRow($query) { $row = mysql_fetch_row($query); return $row; } public function DBArray($query) { $array = mysql_fetch_array($query); return $array; } public function DBCount($query) { $num = mysql_num_rows($query); return $num; } public function DBQuery($sql) { $query = mysql_query($sql) or die(mysql_error()); return $query; } } ?> Index.php: <?php /* @author Sam Granger @copyright 2008 Intrellia. All Rights Reserved. */ include('configuration.php'); include('mysql.class.php'); $DB = new DBConnection($db_server, $db_username, $db_password, $db_database); $sql = 'SELECT title FROM news'; $query = $DB->DBQuery($sql); while($array = $DB->DBArray($query)) { extract($array); echo '<div>$title</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443800 Share on other sites More sharing options...
trq Posted January 19, 2008 Share Posted January 19, 2008 Your __construct is private, making your class unable to be instantiated. You need to make it public. Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443802 Share on other sites More sharing options...
Sam Granger Posted January 19, 2008 Author Share Posted January 19, 2008 Awesome thanks! Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443804 Share on other sites More sharing options...
Barand Posted January 19, 2008 Share Posted January 19, 2008 your references to $query should be $this->query in your methods, otherwise the variable is local to the method. Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443809 Share on other sites More sharing options...
Sam Granger Posted January 19, 2008 Author Share Posted January 19, 2008 So: public function DBRow($query) { $row = mysql_fetch_row($query); return $row; } Should be: public function DBRow($this->query) { $row = mysql_fetch_row($query); return $row; } ? etc... Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443821 Share on other sites More sharing options...
Barand Posted January 19, 2008 Share Posted January 19, 2008 <?php public function DBQuery($sql) { $this->query = mysql_query($sql) or die(mysql_error()); // now stored as object var return $this->query; } public function DBRow() { $row = mysql_fetch_row($this->query); // use stored value return $row; } public function DBArray() { $array = mysql_fetch_array($this->query); return $array; } public function DBCount() { $num = mysql_num_rows($this->query); return $num; } Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443833 Share on other sites More sharing options...
trq Posted January 19, 2008 Share Posted January 19, 2008 query really isn't a good name for that variable either. Result would make more sense. Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443835 Share on other sites More sharing options...
Sam Granger Posted January 19, 2008 Author Share Posted January 19, 2008 Thanks - final question, do need to call the __destruct function or not? Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443839 Share on other sites More sharing options...
trq Posted January 19, 2008 Share Posted January 19, 2008 No, it is called automatically. Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443844 Share on other sites More sharing options...
Sam Granger Posted January 19, 2008 Author Share Posted January 19, 2008 Awesome, thanks! Solved. Will rename query to result as recommended! Ps. Thanks for all the great help guys!! Quote Link to comment https://forums.phpfreaks.com/topic/86790-solved-fatal-error-call-to-private-dbconnection__construct-from-invalid-context/#findComment-443851 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.