I've made a mysql driver that almost works... I've tested it on a page and it always gives me an error and doesn't work the way it should. I set up a test table with 6 entries, the test script should print the entires in 6 table rows - the script creates 6 table rows but it doesn't include the data, and the error returned is saying that it couldn't be fetched...
Here is the test script:
<?php
$root_path = '';
include($root_path . 'includes/extension.inc');
include($root_path . 'includes/global.' . $ext);
// these includes include the driver and create the connection
$db->query("SELECT * FROM driver_test");
echo "<table>\n";
while($test = $db->fetch())
{
echo "<tr><td>$test['id']</td><td>$test['name']</td><td>$test['email']</td></tr>\n";
}
echo "</table>\n\n";
if($db->is_errors())
{
$db->show_errors();
}
?>
And here is the driver itself
class MysqlDriver {
//#######################################################
//# Variables
//#######################################################
var $info = array(
'db_server' => '',
'db_user' => '',
'db_database' => '',
'db_persistent' => false
);
var $error = array();
var $connect_id = 0;
var $query = 0;
var $results = array();
var $query_count = 0;
//#######################################################
//# Functions
//#######################################################
/// Constructor
function MysqlDriver($db_server = '', $db_user = '', $db_pass= '', $db_database = '', $db_persistent = false)
{
if(!($db_server == '' || $db_user == '' || $db_pass == '' || $db_database == ''))
{
$this->info['db_server'] = $db_server;
$this->info['db_user'] = $db_user;
$this->info['db_pass'] = $db_pass;
$this->info['db_database'] = $db_database;
$this->info['db_persistent'] = $db_persistent;
return true;
}
else
{
$this->error[] = 'Insuffient number of arguments supplied to MysqlDriver';
return false;
}
}
//#------------------------------------------------------
//# Connection
//#------------------------------------------------------
/// Connect
function connect()
{
if($this->info['persistent'])
{
$this->connect_id = @mysql_pconnect($this->info['db_server'],
$this->info['db_user'],
$this->info['db_pass']);
}
else
{
$this->connect_id = @mysql_connect($this->info['db_server'],
$this->info['db_user'],
$this->info['db_pass']);
}
if(!($this->connect_id))
{
$this->error[] = 'Could not establish mysql connection.'.$this->get_mysql_error();
return false;
}
elseif(!(mysql_select_db($this->info['db_database'], $this->connect_id)))
{
$this->error[] = 'Could not select mysql database.'.$this->get_mysql_error();
return false;
}
else
{
return true;
}
}
/// Disconnect
function disconnect()
{
if($this->result)
{
@mysql_free_result($this->result);
}
if(!mysql_close($this->connect_id))
{
$this->error[] = 'Could not close mysql connection.'.$this->get_mysql_error();
return false;
}
else
{
return true;
}
}
//#------------------------------------------------------
//# Queries
//#------------------------------------------------------
/// Set Query
function query($query = 0)
{
if(!($query))
{
$this->error[] = 'Insufficient number of arguments supplied to MysqlDriver query()';
return false;
}
else
{
$this->query = @mysql_query($query, $this->connect_id);
if(!($this->query))
{
$this->error[] = 'Could not execute query.'.$this->get_mysql_error();
return false;
}
else
{
return true;
}
}
}
/// Fetch Results
function fetch()
{
$this->results = @mysql_fetch_array($this->query);
if(!($this->results))
{
$this->error[] = 'Could not fetch array.'.$this->get_mysql_error();
return false;
}
else
{
return $this->results;
}
}
/// Free Result
function free_result()
{
if(!(mysql_free_result($this->query)))
{
return false;
}
else
{
return true;
}
}
/// Get Number of Rows
function get_num_rows()
{
return mysql_num_rows($this->query);
}
/// Get Number of Affected Rows
function get_affected_rows()
{
return mysql_affected_rows($this->connect_id);
}
/// Get Number of Queries Used
function get_query_count()
{
return $this->query_count;
}
//#------------------------------------------------------
//# Errors
//#------------------------------------------------------
/// Check for errors
function is_errors()
{
if(count($this->error) >= 1)
{
return true;
}
else
{
return false;
}
}
/// Print out the errors
function show_errors()
{
if(count($this->error) >= 1)
{
echo '<font face="Verdana" size="-1"><u>Warning: The following error(s) have occurred</u><br />';
foreach($this->error as $value)
{
echo '<li>'.$value.'</li>';
}
echo '</font>';
}
}
/// Return Formatted mySQL Errors
function get_mysql_error()
{
$string = ' <small><b>('.@mysql_errno().': '.@mysql_error().')</b></small>';
return $string;
}
}
It seems to be a problem with the fetch, but I dont know...