Jump to content

[SOLVED] Fatal error: Call to private DBConnection::__construct() from invalid context??


Sam Granger

Recommended Posts

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? :(

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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>';
}

?>

Link to comment
Share on other sites

<?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;
}



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.