MySQL database class. Used in every file that accesses the database.
class db {
//--> Class variables, global accessibility
public $mysql_host = '';
public $mysql_user = '';
public $mysql_pass = '';
public $mysql_db = '';
public $mysql_qcount = 0;
public $mysql_debug = 0;
public $error = 0;
function __construct( $sql_host='', $sql_user='', $sql_pass='', $sql_db='', $sql_debug=0 ) {
//--> Class constructor method, set up variables for future transactions
$this->mysql_host = $sql_host;
$this->mysql_user = $sql_user;
$this->mysql_pass = $sql_pass;
$this->mysql_db = $sql_db;
$this->mysql_debug = $sql_debug;
}
function sql_connect( ) {
//--> Class function, connect to MySQL server
$this->mysql_connection_result = mysql_connect( $this->mysql_host, $this->mysql_user, $this->mysql_pass );
if( !$this->mysql_connection_result ) {
$error_msg = "Failed to connect to MySQL server
" . mysql_error( );
die( $error_msg );
}
return 1;
}
function sql_select_db( ) {
//--> Class function, select MySQL database
$this->mysql_dbselect_result = mysql_select_db( $this->mysql_db );
if( !$this->mysql_dbselect_result ) {
$error_msg = "Failed to select database
" . mysql_error( );
die( $error_msg );
}
return 1;
}
function sql_query_db( $sql_query ) {
//--> Class function, query the selected MySQL database
$this->mysql_qcount++;
$this->mysql_query_result = mysql_query( $sql_query );
//echo ( $sql_query );
if( !$this->mysql_query_result ) {
$error_msg = "Query error
" . $sql_query . "
" . mysql_error( );
die( $error_msg );
}
return $this->mysql_query_result;
}
function sql_query_insert_many ( $sql_table, $sql_values ) {
//--> Class function, execute many insert queries
foreach( $sql_values as $vals ) {
$sql_query = "INSERT INTO " . $sql_table . " ( " . $val . " ) ";
$this->sql_query_db ( $sql_query );
}
return 1;
}
function sql_query_ret_obj( $sql_query ) {
//--> Class function [Abstraction of $this->$sql_query_db()], query the selected MySQL database and return object
$result = $this->sql_query_db( $sql_query );
return mysql_fetch_object( $result );
}
function sql_ret_obj ( ) {
return mysql_fetch_object( $this->mysql_query_result );
}
function sql_query_ret_array( $sql_query ) {
//--> Class function [Abstraction of $this->$sql_query_db()], query the selected MySQL database and return object
$result = $this->sql_query_db( $sql_query );
return mysql_fetch_array( $result );
}
function sql_get_row( ) {
//--> Class function, get next row from last query executed
if( !isset( $this->mysql_query_result ) ) {
$error_msg = "sql_get_row() before query
";
die( $error_msg );
}
return mysql_fetch_row( $this->mysql_query_result );
}
function sql_get_num_rows( ) {
//--> Class function, get number of rows resulting from last query
if( !isset( $this->mysql_query_result ) ) {
$error_msg = "sql_get_num_rows before query
";
die( $error_msg );
}
return mysql_affected_rows( $this->mysql_query_result );
}
function sql_get_last_autoid( ) {
//--> Class function, get auto assigned id given to the last INSERT query
if( !isset( $this->mysql_query_result ) ) {
$error_msg = "sql_get_last_autoid before query
" . mysql_error( );
die( $error_msg );
}
return mysql_insert_id( $this->mysql_query_result );
}
}
?>