I am working through a Harry Fuecks (sitepoint) example and I keep getting the following error:
Fatal error: Call to a member function on a non-object in /var/www/html/mysite/class/5.php on line 12 .. have looked around through other examples, but can't figure out what it is... please help.
5.php
<?php
// include
require_once '//include/mysite/dbConnect.php';
$db = &new MySQL();
$sql = "SELECT * FROM tb_entries ORDER BY thedate";
//perform
$result = $db->query($sql);
//Iterate through the results
while ($row = $result->fetch()) {
echo 'ftime: ' .$row['ftime'] . '<br />';
echo 'ltime: ' .$row['ltime'] . '<br />';
echo 'project_id: ' .$row['project_id'] . '<br />';
}
?>
and the include file (dbConnect.php):
<?php
//
class MySQL {
var $host;
var $dbUser;
var $dbPass;
var $dbName;
var $dbConn;
var $connectError;
//constructor
function MySQL()
{
$this->host = 'myhost';
$this->dbUser = 'myuser';
$this->dbPass = 'mypass';
$this->dbName = 'mydbname';
$this->connectToDb();
}
// establish connection, select db
function connectToDb()
{
if (!$this->dbConn = @mysql_connect($this->host, $this->dbUser, $this->dbPass)) {
trigger_error('Could not connect to server');
$this->connectError = true;
} else if (!@mysql_select_db($this->dbName, $this->dbConn)) {
trigger_error('Could not select database');
$this->connectError = true;
}
}
// checks for MySQL errors
function isError()
{
if ($this->connectError) {
return true;
}
$error = mysql_error($this->dbConn);
if (empty($error)) {
return false;
} else {
return true;
}
}
// returns an instance of MySQLResult
function &query($sql)
{
if (!$queryResource = mysql_query($sql, $this->dbConn)) {
trigger_error('Query failed: ' . mysql_error($this->dbConn) . ' SQL: ' . $sql);
new MySQLResult($this, $queryResource);
}
}
}
// MySQLResult Fetching class
class MySQLResult{
var $mysql;
var $query;
// class constructor
function MySQLResult(&$mysql, $query)
{
$this->mysql = &$mysql;
$this->query = $query;
}
//fetches a row from result
function fetch()
{
if ($row = mysql_fetch_array($this->query, MYSQL_ASSOC)) {
return $row;
} else if ( $this->size() > 0 ) {
mysql_data_seek($this->query, 0);
return false;
} else {
return false;
}
}
// checks for MySQL errors
function isError()
{
return $this->mysql->isError();
}
}
?>