Click to See Complete Forum and Search --> : New MySQL class - Give suggests to improve this
Y.P.Y
Oct 23rd, 2008, 08:11 AM
_______________________________
dclamp
Oct 23rd, 2008, 06:13 PM
Why make a mysql class when PHP has built in mysql functions already?
penagate
Oct 24th, 2008, 12:56 AM
The first improvement you can make is to indent your code.
The second improvement is to throw it away and use PDO.
Y.P.Y
Oct 24th, 2008, 03:43 AM
Dclamp: Why make a MySQL class when PHP has built in MySQL functions already?
:-O
1- Fastness
2- Secure
3- Prefect
4- Exactitude
5- Arrangment and regularity
6- Using in anywhere for anytimes. No 1 time.
7- 1 line code
And...
More? ;)
On line code for any times: YPY_MySQL::_Excute_Query('...');
? lines codes for ? times:
if(!is_string($Str_Query) || $this->_Is_Blank($Str_Query) || !$this->_Is_Ready())
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Invalid query/Is not ready.');
@exit('')
};
_Free_Result();
$_Bln_Free_Result= true;
$Str_Query= @strval($Str_Query);
$Str_Query= @$this->_Clean_Value($Str_Query);
if($Bln_RemoveTag): @$this->_Remove_Tag($Str_Query);
endif;
if($Bln_Unbuffered): $_Prv_Int_QueryIdentity= @mysql_unbuffered_query($Str_Query, $_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not excute unbuffered query(#' . @mysql_errno($_Prv_Int_ConnectIdentity) . ').');
else: $_Prv_Int_QueryIdentity= @mysql_query($Str_Query, $_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not excute query(#' . @mysql_errno($_Prv_Int_ConnectIdentity) . ').');
endif;
$_Prv_Int_QueryIdentity ? $_Pub_Bln_QueryExcuted= true : $_Pub_Bln_QueryExcuted= false;
$_Pub_Bln_QueryExcuted && $_Pub_Bln_RowsAffected ? (boolean)true : (boolean)false;
...
@mysql_query($Str_Query);
penagate
Oct 24th, 2008, 06:19 AM
Those reasons are bogus. I think you've just come up with the attributes of good code and listed them out three times each, in descending order of sanity.
What, pray, is faster or more secure about this:
YPY_MySQL::_Excute_Query('drop schema xyz');
than this?
mysql_query('drop schema syz');
I know exactly what the second line does. I can guess what the first does, but to be sure, I would have to waste time deciphering your unreadable code. (Your naming convention and lack of indentation has done a better job than some code obfuscation tools.)
I_Love_My_Vans
Oct 24th, 2008, 06:47 AM
I agree with pena, unless your writing a single function to santise against injection etc, this is purely reinventing the wheel.
And again, before you go trying to take over the world, learn about good coding practices, such as code indentation and naming conventions. Here is a good example, http://www.vbforums.com/showpost.php?p=2440249&postcount=7, the author is a bit weird tho...
7- 1 line of code
Well, it clearly isn't...
<?
@error_reporting(E_ALL ^ E_NOTICE);
if(!extension_loaded('standard')): @exit('Extension Standard not loaded!');
endif;
if(!extension_loaded('mysql')): @exit('Extension MySQL not loaded!');
endif;
if(!class_exists('YPY_MySQL')): @exit('Class YPY_MySQL not exists!');
endif;
@set_magic_quotes_runtime(0);
class YPY_MySQL
{
private $_Prv_Str_Hostname= '';
private $_Prv_Int_Port= 3306;
private $_Prv_Str_Databasename= '';
private $_Prv_Str_Username= '';
private $_Prv_Str_Password= '';
private $_Prv_Str_TablePrefix= '';
private $_Prv_Str_Quote= '`';
private $_Prv_Int_ConnectIdentity= -1;
private $_Prv_Int_SelectIdentity= -1;
private $_Prv_Int_CloseIdentity= -1;
private $_Prv_Int_QueryIdentity= -1;
private $_Prv_Int_AffectedRowsIdentity= -1;
private $_Prv_Ary_MySQLFunctions= array('mysql_connect', 'mysql_pconnect', 'mysql_close', 'mysql_select_db', 'mysql_query', 'mysql_unbuffered_query', 'mysql_error', 'mysql_errno', 'mysql_affected_rows', 'mysql_insert_id', 'mysql_result', 'mysql_num_rows', 'mysql_num_fields', 'mysql_fetch_row', 'mysql_fetch_array', 'mysql_fetch_assoc', 'mysql_fetch_object', 'mysql_data_seek', 'mysql_fetch_field', 'mysql_field_seek', 'mysql_free_result', 'mysql_field_name', 'mysql_field_len', 'mysql_field_type', 'mysql_escape_string', 'mysql_client_encoding', 'mysql_get_client_info', 'mysql_set_charset', 'mysql_db_name', 'mysql_tablename');
private $_Prv_Ary_Result= array(0);
public $_Pub_Ary_Event= array(0);
public $_Pub_Bln_QueryExcuted;
public $_Pub_Bln_RowsAffected;
final public function __clone()
{
return(@exit('Cloning is not allowed.'));
}
final public function _Initialize($Str_Hostname, $Int_Port= 3306, $Str_Databasename= 'database', $Str_Username= 'root', $Str_Password= '', $Str_TablePrefix= '')
{
if(!is_string($Str_Hostname) || !is_integer($Int_Port) || !is_string($Str_Databasename) || !is_string($Str_Username) || !is_string($Str_Password) || !is_string($Str_TablePrefix) || strlen((string)$Str_Hostname) > 255 || strlen((string)$Int_Port) > 10 || strlen((string)$Str_Databasename) > 255 || strlen((string)$Str_Username) > 255 || strlen((string)$Str_Password) > 255 || strlen((string)$Str_TablePrefix) > 255 || $this->_Is_Blank($Str_Hostname))
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Information is not valid.');
return((boolean)false);
};
if(!$this->_Is_Blank($Str_TablePrefix) && !@$this->_Is_Valid_Table_Prefix($Str_TablePrefix))
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Table prefix is not valid.');
return((boolean)false);
};
$this->_Initialize= false;
$this->_Prv_Str_Hostname= @$this->_Remove_Tag(@strtolower($Str_Hostname));
$this->_Prv_Int_Port= abs(@$this->$Int_Port);
$this->_Prv_Str_Databasename= @$this->_Remove_Tag(strtolower($Str_Databasename));
$this->_Prv_Str_Username= @$this->_Remove_Tag($Str_Username);
$this->_Prv_Str_Password= @$this->_Remove_Tag($Str_Password);
$this->_Prv_Str_TablePrefix= @$this->_Remove_Tag(@strtolower($Str_TablePrefix));
@mysql_close(-1);
@mysql_close(0);
@mysql_close($this->_Prv_Int_ConnectIdentity);
@$this->_Free_Result();
$this->_Prv_Int_ConnectIdentity= -1;
$this->_Prv_Int_SelectIdentity= -1;
$this->_Prv_Int_CloseIdentity= -1;
$this->_Prv_Int_QueryIdentity= -1;
$this->_Prv_Int_AffectedRowsIdentity= -1;
$this->_Prv_Ary_Result= array(0);
$this->_Connect= false;
$this->_Close= false;
$this->_Select= false;
$this->_Pub_Bln_QueryExcuted;
$this->_Free_Result= true;
$this->_Pub_Bln_RowsAffected;
$this->_Initialize= true;
return((boolean)$this->_Initialize);
}
final public function _Connect($Bln_Persistency= false, $Bln_New= true)
{
if(!$this->_Initialize)
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Information is not initialized.');
return((boolean)false);
};
if($Bln_Persistency): $this->_Prv_Int_ConnectIdentity= @mysql_pconnect($this->_Prv_Str_Hostname . ':' . $this->_Prv_Int_Port, $this->_Prv_Str_Username, $this->_Prv_Str_Password) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Unable to connect(Persistency) to database(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
else: $this->_Prv_Int_ConnectIdentity= @mysql_connect($this->_Prv_Str_Hostname . ':' . $this->_Prv_Int_Port, $this->_Prv_Str_Username, $this->_Prv_Str_Password, $Bln_New) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Unable to connect to database(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
endif;
$this->_Prv_Int_ConnectIdentity ? $this->_Connect= true : $this->_Connect= false;
return((boolean)$this->_Connect);
}
final public function _Close()
{
@$this->_Free_Result() ? $this->_Free_Result= true : $this->_Free_Result= false;
$this->_Prv_Int_CloseIdentity= @mysql_close($this->_Prv_Int_ConnectIdentity) ? $this->_Prv_Int_ConnectIdentity= -1 : $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not close connection/database(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
$this->_Prv_Int_CloseIdentity ? $this->_Close= true : $this->_Close= false;
return((boolean)$this->_Close);
}
final public function _Select()
{
if(!$this->_Initialize || !$this->_Connect || $this->_Close)
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Not initialized/Not connected.');
return((boolean)false);
};
$this->_Prv_Int_SelectIdentity= @mysql_select_db($this->_Prv_Str_Databasename, $this->_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Unable to select database(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
$this->_Prv_Int_SelectIdentity ? $this->_Select= true : $this->_Select= false;
if($this->_Select)
{
@$this->_Free_Result();
$this->_Free_Result= true;
};
return((boolean)$this->_Select);
}
final public function _Excute_Query($Str_Query, $Bln_RemoveTag= false, $Bln_Unbuffered= false)
{
if(!is_string($Str_Query) || $this->_Is_Blank($Str_Query) || !$this->_Is_Ready())
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Invalid query/Is not ready.');
return((boolean)false);
};
@$this->_Free_Result();
$this->_Free_Result= true;
$Str_Query= @strval($Str_Query);
#$Str_Query= @$this->_Clean_Value($Str_Query);
if($Bln_RemoveTag): @$this->_Remove_Tag($Str_Query);
endif;
if($Bln_Unbuffered): $this->_Prv_Int_QueryIdentity= @mysql_unbuffered_query($Str_Query, $this->_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not excute unbuffered query(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
else: $this->_Prv_Int_QueryIdentity= @mysql_query($Str_Query, $this->_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not excute query(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
endif;
$this->_Prv_Int_QueryIdentity ? $this->_Pub_Bln_QueryExcuted= true : $this->_Pub_Bln_QueryExcuted= false;
return($this->_Pub_Bln_QueryExcuted && $this->_Pub_Bln_RowsAffected) ? (boolean)true : (boolean)false;
}
final public function _Affect_Rows()
{
if(!$this->_Is_Ready())
{
$this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Is not ready.');
return((boolean)false);
};
$this->_Prv_Int_AffectedRowsIdentity= @mysql_affected_rows($this->_Prv_Int_ConnectIdentity) or $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Could not affect row(s)(#' . @mysql_errno($this->_Prv_Int_ConnectIdentity) . ').');
$this->_Prv_Int_AffectedRowsIdentity ? $this->_Pub_Bln_RowsAffected= true : $this->_Pub_Bln_RowsAffected= false;
return((boolean)$this->_Pub_Bln_RowsAffected);
}
final public function _Free_Result()
{
@mysql_free_result($this->_Prv_Int_QueryIdentity) ? $this->_Free_Result= true : $this->_Free_Result= false;
if(!$this->_Free_Result): $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Unable to free query result.');
endif;
return((boolean)$this->_Free_Result);
}
final private function _Clean_Table_Name($Str_Input)
{
if(!is_string($Str_Input) || $this->_Is_Blank($Str_Input)): return((boolean)false);
endif;
get_magic_quotes_gpc() ? $Str_Input= @stripslashes($Str_Input) : $Str_Input= @addslashes($Str_Input);
if(function_exists('mysql_real_escape_string')): @mysql_real_escape_string($Str_Input, $this->Int_QueryIdentity);
else: @mysql_escape_string($Str_Input);
endif;
return((string)@htmlspecialchars($Str_Input));
}
final private function _Clean_Value($Str_Input)
{
get_magic_quotes_gpc() ? $Str_Input= @stripslashes($Str_Input) : $Str_Input= @addslashes($Str_Input);
if(function_exists('mysql_real_escape_string')): @mysql_real_escape_string($Str_Input, $this->Int_QueryIdentity);
else: @mysql_escape_string($Str_Input);
endif;
return((string)$Str_Input);
}
final private function _Is_Valid_Table_Prefix($Str_Input)
{
return(preg_match('|[^a-zA-Z0-9_]|i', $Str_Input) ? (boolean)true : (boolean)false);
}
final private function _Is_Ready()
{
return($this->_Initialize && $this->_Connect && $this->_Prv_Int_ConnectIdentity && !$this->_Close && $this->_Select) ? (boolean)true : (boolean)false;
}
final private function _Throw_Event()
{
}
final private function _Is_Blank($Str_Input)
{
return(empty($Str_Input) || is_null($Str_Input) || strlen((string)$Str_Input) <= 0) ? (boolean)true : (boolean)false;
}
};
?>
Y.P.Y
Oct 24th, 2008, 06:53 AM
Penagate, i dont know PDO. :(.
I_Love_My_Vans, this class is not for small/simple projects. This is for long/heavy projects.
I_Love_My_Vans
Oct 24th, 2008, 07:29 AM
Penagate, i dont know PDO. :(.
I_Love_My_Vans, this class is not for small/simple projects. This is for long/heavy projects.
Regardless of the project, the question Dylan asked was what is the benefit of using your own class rather then the built in MySQL functions within PHP, and the answers you gave apply to the built in functions, at what point is it faster to use x times more code?
I can understand building a function like this perhaps increase your knowledge of how classes are created, but so far there has been no real evidence of your class being any better than a batch of small pox. And the reason why I haven't bowed down to you is because I cannot read your code to find out for myself.
It's good that your attempting to increase the efficiency of a problem, but I can see no reason to when all of your points can be covered using the built in functions.
visualAd
Oct 24th, 2008, 04:21 PM
Penagate, i dont know PDO. :(.
I_Love_My_Vans, this class is not for small/simple projects. This is for long/heavy projects.
If you don't know PDO, then why not read up the documentation and learn it. Its not rocket science and would be much better suited to large projects that the cryptic tosh that you posted in your first post.
There is not reason, at all, why you need to waste your time creating a MySql wrapper, when it has already been done for you. I created one in the PHP5 OOP tutorials you see in my signature to demonstrate the object orientated tools available in PHP 5; again though, it is only for demonstration purposes but if you follow a tutorial and learn some good coding practices then the time you spend reinventing the wheel will be a lot better used.
dclamp
Oct 24th, 2008, 05:57 PM
:) thanks for expanding on my question.
the182guy
Oct 27th, 2008, 12:03 PM
There really is no need to create your own MySQL class, instead of that use the built in MySQLi class (http://uk2.php.net/mysqli). It has all the mysql functions built in as methods so is a good OOP approach.
You will probably say well it doesn't do custom functionality that I need to do, thats easy just extend the class yourself and add your own functions or override the function you want to modify.
Example:
$db= new mysqli("localhost", "my_user", "my_password", "my_db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$res = $db->query("SELECT * FROM customers");
echo "found customers: " . $db->affected_rows;
while($customer = $res->fetch_array())
{
print_r($customer);
}
StrangerInBeijing
Oct 30th, 2008, 06:32 AM
The first improvement you can make is to indent your code.
The second improvement is to throw it away and use PDO.
ROFLMFAO......hahaha..... PML :lol:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.