Help optimising this MySQL wrapper
Posted: Mon Jul 23, 2007 10:10 am
I found this MySql in one tutorial, but it doesnt realy look `smoth` to me for now.
What could I add, update, fix to make it faster / more optimised.
What could I add, update, fix to make it faster / more optimised.
Code: Select all
<?php
class sql
{
var $dbhost;
var $dbuser;
var $dbpass;
var $dbase;
var $sql_query;
var $mysql_link;
var $sql_result;
var $query_count;
function sql()
{
$this->dbhost = '***';
$this->dbuser = '***';
$this->dbpass = '***';
$this->dbase = '***';
$this->mysql_link = '0';
$this->query_count = '0';
$this->sql_result = '';
$this->connection();
}
function connection()
{
$this->mysql_link = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpass ) or $this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );
@mysql_select_db( $this->dbase ) or $this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );
}
function error( $error_msg, $sql_query, $error_no )
{
$error_date = date("F j, Y, g:i a");
//Heredoc Strings must be on the far left lines
$error_page = <<<EOD
<html><head><title>mySQL database Error</title>
<style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body>
<br><br><blockquote><b>There appears to be an error while trying to complete your request.</b><br>
You can try to go back and try again by clicking <a href="javascript:history.go(-1);">here</a>, if you
still get the error you can contact the site administrator by clicking <a href='mailto:spamyboy@gmail.com?subject=SQL+Error+lyricing'>here</a>
<br><br><b>Error Returned</b><br>
<form name='mysql'><textarea rows="15" cols="60">mySQL query error: {$sql_query}
mySQL error: {$error_msg}
mySQL error code: {$error_no}
Date: {$error_date}</textarea></form><br>We apologise for any inconvenience</blockquote></body></html>
EOD;
//echo $error_page;
}
function close()
{
mysql_close( $this->mysql_link );
}
function sql_query( $sql_query )
{
$this->sql_query = $sql_query;
$this->sql_result = @mysql_query( $sql_query, $this->mysql_link );
if (!$this->sql_result)
{
$this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );
}
$count = $this->query_count;
$count = ($count + 1);
$this->query_count = $count;
}
function num_rows()
{
$mysql_rows = mysql_num_rows( $this->sql_result );
if (!$mysql_rows)
{
$this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );
}
return $mysql_rows;
}
function affected_rows()
{
$mysql_affected_rows = mysql_affected_rows( $this->mysql_link );
return $mysql_affected_rows;
}
function fetch_array()
{
if ( $this->num_rows() > 0 )
{
$mysql_array = mysql_fetch_assoc( $this->sql_result );
if (!is_array( $mysql_array ))
{
return FALSE;
}
return $mysql_array;
}
}
function fetch_rows()
{
if ( $this->num_rows() > 0 )
{
$mysql_array = mysql_fetch_row( $this->sql_result );
if (!is_array( $mysql_array ))
{
return FALSE;
}
return $mysql_array;
}
function query_count()
{
return $this->query_count;
}
}
}
?>