Jump to content

Recommended Posts

Good morning,

 

I have this class saved in "class_lib/db.php":

<?php
class DBManager {
protected $_connection;
private $_type;

function __construct($db, $host, $username, $password) {
	$this->_type = $db;
	if ($this->_type == 'mysql') {
		$this->_connection = mysql_connect($host, $username, $password);
		mysql_query('SET NAMES UTF8');
		mysql_query('SET CHARACTER_SET utf8');
	}
}

function dbRead($query) {
	if ($this->_type == 'mysql') {
		$resultat = mysql_query($query, $this->_connection);
		for ($i = 0; $i < mysql_num_rows($resultat); $i++) {
			$array[$i] = mysql_fetch_array($resultat, MYSQL_ASSOC);
		}
	}
	return $array;
}

function dbWrite($query) {
	if ($this->_type == 'mysql') {
		$sql = mysql_query($query);
	}
}
}

 

I also have this instantiation:

<?php
include_once('class_lib/db.php');

$DBManager = new DBManager('mysql', 'host.example.com', 'xxxxxxxx', 'xxxxxxxxxxxx');
print '<pre>';
print_r($DBManager -> dbRead('SELECT * FROM db.table'));
print '</pre>';

 

I get this error:

"Fatal error: Cannot access empty property in /home/category/class_lib/db.php on line 9"

 

Notice:

i only get the error if I try and include the class from a separate file. If I copy the class to the view, I get no error.

 

The question:

Why?

Link to comment
https://forums.phpfreaks.com/topic/195286-class-error-when-included/
Share on other sites

Your code works for me.

 

Best guess is that the actual code being executed is not what you think it is. You either have more than one class_lib/db.php folder/file and the wrong one is being included (due to the php include_path setting) or when you uploaded the db.php file it did not work and an older version of the code is what is being used.

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.