Code: Select all
$mysql = new MySQL();
$mysql->selectDatabase('test');
$result = mysql_query('SELECT * FROM test');Code: Select all
$mysql = new MySQL();
$mysql->selectDatabase('test');
$result = new Iterator('SELECT * FROM test');I have done a previous class - which I unfortunately lost it in a format -> I swear I backed it up but oh well. Ouch
I've been scratching my head and tearing my hair for a day so maybe I'm missing something. Please do advise. If you do pick up and tips/bugs for me - also do tell. Thanks.
-Nay
The code:
Code: Select all
<?php
// MySQL class to handle MySQL database connections and database selections
Class MySQL {
var $mysql_link;
var $mysql_db;
// Constructor
function MySQL($flag = 0) {
// return (void)
// (Optional) (int) $flag = Flag 0 (False) or 1 (True) to select the
// default MySQL database after connecting to MySQL
// Require Config file
require('class_config.php');
$Config = new Config();
// Set local variables
$mysql_host = $Config->getVar('mysql_host');
$mysql_user = $Config->getVar('mysql_user');
$mysql_pass = $Config->getVar('mysql_pass');
// If unable to get variable values:
if( !empty($mysql_host) && !empty($mysql_user) && !empty($mysql_pass) ) {
die('Could not get requied variables for connection in MySQL::MySQL().');
}
// Connect to MySQL
$tmp_link = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
// If failed to connect to MySQL
if( empty($tmp_link) ) {
die('Could not connect to MySQL in MySQL:MySQL(). MySQL returned:<br />' . mysql_error());
}
// Set connection link
$this->mysql_link = $tmp_link;
// If flag is set, select default database
if($flag == 1) {
// Get default database name, and select
$mysql_df_db = $Config->getVar('mysql_df_db');
$this->selectDatabase($mysql_df_db);
}
}
function selectDatabase($db) {
// return (bool)
// (str) $db = The name of the database to select
if( empty($db) ) {
die('Error: Argument not given or is empty for MySQL::selectDatabase()');
}
// Select database
$tmp_select = mysql_select_db($db, $this->mysql_link);
// If unable to select database:
if( empty($tmp_select) ) {
die('Error: Could not select database ' . $db . ' in MySQL::selectDatabase(). MySQL returned: <br />' . mysql_error());
}
// Set selection
$this->mysql_db = $tmp_select;
return $true;
}
}
// Iterator Class, child class of MySQL - to handle any MySQL queries
Class Iterator extends MySQL {
// Class variables
var $last_query;
var $last_result;
var $last_rows; // Number of rows of last query
var $last_array; // From mysql_fetch_array($last_result);
// Variable for Iterator::nextResult();
var $counter = -1;
// Constructor
function Iterator($query = '') {
// return (void)
// (Optional) (str) $query = The MySQL query string to query.
// Check if connected to MySQL
if( empty($this->mysql_link) ) {
die('Unable to instantiate Iterator class - no MySQL connection present.');
}
// If the query is not empty, query and fetch result array
if( !empty($query) ) {
$this->queryFetch($query);
}
}
function queryFetch($query) {
// return(int)
// (str) $query = MySQL query string (eg: 'SELECT name FROM admins')
// Check is query is empty
if( empty($query) ) {
die('Query given for Iterator::queryFetch is empty');
}
// Query given string
$tmp_result = mysql_query($query, $this->mysql_link);
// If query failed to execute/returned error
if( empty($tmp_result) ) {
die('MySQL query failed in Iterator::queryFetch() for query:<br /> ' . $query . '<br />MySQL returned:<br />' . mysql_error());
}
// Check if it is an 'SELECT' query:
if ( preg_match('^SELECT', $query) ) {
// If so, fetch a result array, get number of rows
// Set number of rows
$this->last_rows = mysql_num_rows($tmp_result) or die('Could not get number of rows in Iterator::queryFetch()');
// Set result array
while( $tmp_row = mysql_fetch_assoc($tmp_result) or die('Error getting result row in Iterator:queryFetch()') ) {
$this->last_array[] = $tmp_row;
}
}
return $tmp_result;
}
function nextResult() {
// return (bool)
// If there is no result array to iterate:
if( empty($this->last_array) ) {
die('Iterator->last_array is empty. Unable to continue with function Iterator::nextResult()');
}
// Add reference and add to counter
$counter =& $this->counter;
$counter++;
// Determine return true or false, for use in while loop (eg: while($row = $Iterator->nextResult) { print_r($row); }
if( empty($this->last_array[$counter]) ) {
// If there is no more results left
return false;
} else {
// Else return a row
return $this->last_array[$counter];
}
}
}
?>