Forum Moderators: coopster

Message Too Old, No Replies

PHP - OOP - Call to Undefined Method.

         

duckxtales

5:44 am on Feb 12, 2007 (gmt 0)

10+ Year Member



I have the following code, but I am having a call the undefined method error on the line with: __query($zzz)

Am I doing something wrong here? The class is working fine, its just that i need to get used to the code and I'm not sure if im programming this properly using oop.

$database = "vbox";
$dbmod = New database;
$dbmod->__construct();

$zzz = "show tables";
$dbmod->__query($zzz);
$test = $dbmod->sqlError;
echo $test;

Psychopsia

6:12 am on Feb 12, 2007 (gmt 0)

10+ Year Member



Hi!
The name of class is "vbox" or "database"?
Are you sure the method exists?

duckxtales

6:48 am on Feb 12, 2007 (gmt 0)

10+ Year Member



the class is called database and vbox is just the name of the database im accessing. and under the class database has the function query. class is pasted below:

class database {

private $database= false;
public $sqlError= false;
public $rowCount= false;
public $insertID= false;

public function __construct() {
global $database;
$this->database = $database;
if (!mysql_connect()) $this->sqlError = 'Cannot connect to the database because: '.mysql_error();
elseif (!mysql_select_db($database)) $this->sqlError = 'Cannot select database because: '.mysql_error();
}

public function __destruct() {
mysql_close();
}

public function query($query) {
$this->sqlError = false;
$this->foundRows = false;
$this->rowCount = false;
$this->insertID = false;
$result = mysql_query($query);
if (mysql_error()) {
$this->sqlError = 'Query error: ' . mysql_error();
debug($query.'<br>'.$this->sqlError);
return false;
} else {
$this->rowCount = strpos($query, 'SELECT') === 0? mysql_num_rows($result) : mysql_affected_rows();
if (strpos($query, 'INSERT') === 0) $this->insertID = mysql_insert_id();
if (strpos($query, 'SQL_CALC_FOUND_ROWS')!== false) {
$this->foundRows = mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0);
}
debug($query.'<br>Found Rows: '.$this->foundRows.' ¦ Row Count: '.$this->rowCount.' ¦ Insert ID: '.$this->insertID);
return $result;
}
}

} // class database

dreamcatcher

8:41 am on Feb 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a thought, but do you have PHP5 installed?

dc

ericjust

8:51 am on Feb 12, 2007 (gmt 0)

10+ Year Member



Change:

$dbmod->__query($zzz);

To:

$dbmod->query($zzz);

P.S.

1. You do not have to call the __contruct method. This method will be called automatically on class initialization.

2. Change "$dbmod = New database;" to "$dbmod = new database();"

duckxtales

6:54 pm on Feb 12, 2007 (gmt 0)

10+ Year Member



thanks.. lol stupid me, I didnt read the function correctly. As for __construct being called automatically, is this standard for all classes that __construct is called automatically? And yes I'm using PHP5

coopster

11:16 pm on Feb 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



As for __construct being called automatically, is this standard for all classes that __construct is called automatically?

For PHP5, yes, unless the parent class has been extended.
Constructors and Destructors [php.net]