I'm getting the following error:
Fatal error: Call to a member function query() on a non-object
My class code:
<?php
require_once '/home/dottedqu/php/MDB2.php';
class mdb2cls {
function _construct () {
}
public function dataSourceName($dbtype, $user_name, $password, $host, $database, $dsnopts, $connectfunct) {
//set dns string
switch ($dbtype) {
case "mysqli":
$dsn = "mysqli://$user_name:$password@$host/$database";
break;
}
$options = $dsnopts;
switch ($connectfunct) {
case "factory":
$mdb2 =& MDB2::factory($dsn, $options);
break;
case "connect":
$mdb2 =& MDB2::connect($dsn, $options);
break;
case "singleton":
$mdb2 =& MDB2::singleton($dsn, $options);
break;
default:
$mdb2 =& MDB2::factory($dsn, $options);
break;
}
//check for errors
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage() . ', ' . $mdb2->getDebugInfo());
}
//$mdb2->disconnect();
}
public function runQuery($type, $sql) {
$query = $type . ' ' . $sql;
switch ($type) {
case "select";
// Proceed with a select query
$res =& $mdb2->query($query); //Fatal Error produced here!!!
break;
case "insert";
// Proceed with insert query
$affected =& $mdb2->exec($query);
default:
echo "runquery error";
}
// Always check that result is not an error
if (PEAR::isError($affected)) {
die($affected->getMessage());
}
// Disconnect
//$mdb2->disconnect();
}
public function fetchRes() {
// Get each row of data on each iteration until
// there are no more rows
while (($row = $res->fetchRow())) {
// Assuming MDB2's default fetchmode is MDB2_FETCHMODE_ORDERED
echo $row[0] . "\n";
}
}
}
?>
login.php code:
if ($form->validate())
{
$mysql = new mdb2cls;
$mysql->dataSourceName(
"mysqli",
"*****",
"*****",
"localhost",
"*****",
array(
'debug' => 2,
'result_buffering' => false
),
"factory"
);
$mysql->runQuery ('select', 'SELECT * FROM `webpanel_accounts`');
//$mysql->connectobj();
//Out put the results
$mysql->fetchRes();
}
else
{
$form->display();
}
I'm brand new to OOP programing. I'm pretty sure I know what the issue is but have no idea how to fix it. I think the reason why i'm receiving that error is, there is no initiated object by the name $MDB2 within runQuery function? Like I said that just a guess. Any help/suggestions on what I should do to solve this error is greatly appreciated.
-Thanks,
Rich