Results 1 to 12 of 12

Thread: New MySQL class - Give suggests to improve this

  1. #1

    Thread Starter
    Lively Member Y.P.Y's Avatar
    Join Date
    Sep 2008
    Location
    Tehran - Iran
    Posts
    88

    Lightbulb _______________________________

    _______________________________
    Last edited by Y.P.Y; Apr 28th, 2012 at 05:16 PM.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: New MySQL class - Give suggests to improve this

    Why make a mysql class when PHP has built in mysql functions already?
    My usual boring signature: Something

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: New MySQL class - Give suggests to improve this

    The first improvement you can make is to indent your code.
    The second improvement is to throw it away and use PDO.

  4. #4

    Thread Starter
    Lively Member Y.P.Y's Avatar
    Join Date
    Sep 2008
    Location
    Tehran - Iran
    Posts
    88

    Re: New MySQL class - Give suggests to improve this

    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:
    Code:
    YPY_MySQL::_Excute_Query('...');
    ? lines codes for ? times:
    Code:
    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);

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: New MySQL class - Give suggests to improve this

    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:
    PHP Code:
    YPY_MySQL::_Excute_Query('drop schema xyz'); 
    than this?
    PHP Code:
    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.)

  6. #6
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: New MySQL class - Give suggests to improve this

    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...49&postcount=7, the author is a bit weird tho...

    Quote Originally Posted by Y.P.Y
    7- 1 line of code
    Well, it clearly isn't...

    php Code:
    1. <?
    2. @error_reporting(E_ALL ^ E_NOTICE);
    3.  
    4. if(!extension_loaded('standard')): @exit('Extension Standard not loaded!');
    5. endif;
    6. if(!extension_loaded('mysql')): @exit('Extension MySQL not loaded!');
    7. endif;
    8. if(!class_exists('YPY_MySQL')): @exit('Class YPY_MySQL not exists!');
    9. endif;
    10.  
    11. @set_magic_quotes_runtime(0);
    12.  
    13. class YPY_MySQL
    14. {
    15. private $_Prv_Str_Hostname= '';
    16. private $_Prv_Int_Port= 3306;
    17. private $_Prv_Str_Databasename= '';
    18. private $_Prv_Str_Username= '';
    19. private $_Prv_Str_Password= '';
    20. private $_Prv_Str_TablePrefix= '';
    21. private $_Prv_Str_Quote= '`';
    22.  
    23. private $_Prv_Int_ConnectIdentity= -1;
    24. private $_Prv_Int_SelectIdentity= -1;
    25. private $_Prv_Int_CloseIdentity= -1;
    26. private $_Prv_Int_QueryIdentity= -1;
    27. private $_Prv_Int_AffectedRowsIdentity= -1;
    28. 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');
    29. private $_Prv_Ary_Result= array(0);
    30.  
    31. public $_Pub_Ary_Event= array(0);
    32. public $_Pub_Bln_QueryExcuted;
    33. public $_Pub_Bln_RowsAffected;
    34.  
    35. final public function __clone()
    36. {
    37. return(@exit('Cloning is not allowed.'));
    38. }
    39.  
    40. final public function _Initialize($Str_Hostname, $Int_Port= 3306, $Str_Databasename= 'database', $Str_Username= 'root', $Str_Password= '', $Str_TablePrefix= '')
    41. {
    42.  
    43. 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))
    44. {
    45. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Information is not valid.');
    46. return((boolean)false);
    47. };
    48.  
    49. if(!$this->_Is_Blank($Str_TablePrefix) && !@$this->_Is_Valid_Table_Prefix($Str_TablePrefix))
    50. {
    51. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Table prefix is not valid.');
    52. return((boolean)false);
    53. };
    54.  
    55. $this->_Initialize= false;
    56. $this->_Prv_Str_Hostname= @$this->_Remove_Tag(@strtolower($Str_Hostname));
    57. $this->_Prv_Int_Port= abs(@$this->$Int_Port);
    58. $this->_Prv_Str_Databasename= @$this->_Remove_Tag(strtolower($Str_Databasename));
    59. $this->_Prv_Str_Username= @$this->_Remove_Tag($Str_Username);
    60. $this->_Prv_Str_Password= @$this->_Remove_Tag($Str_Password);
    61. $this->_Prv_Str_TablePrefix= @$this->_Remove_Tag(@strtolower($Str_TablePrefix));
    62. @mysql_close(-1);
    63. @mysql_close(0);
    64. @mysql_close($this->_Prv_Int_ConnectIdentity);
    65. @$this->_Free_Result();
    66. $this->_Prv_Int_ConnectIdentity= -1;
    67. $this->_Prv_Int_SelectIdentity= -1;
    68. $this->_Prv_Int_CloseIdentity= -1;
    69. $this->_Prv_Int_QueryIdentity= -1;
    70. $this->_Prv_Int_AffectedRowsIdentity= -1;
    71. $this->_Prv_Ary_Result= array(0);
    72. $this->_Connect= false;
    73. $this->_Close= false;
    74. $this->_Select= false;
    75. $this->_Pub_Bln_QueryExcuted;
    76. $this->_Free_Result= true;
    77. $this->_Pub_Bln_RowsAffected;
    78. $this->_Initialize= true;
    79. return((boolean)$this->_Initialize);
    80. }
    81.  
    82. final public function _Connect($Bln_Persistency= false, $Bln_New= true)
    83. {
    84.  
    85. if(!$this->_Initialize)
    86. {
    87. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Information is not initialized.');
    88. return((boolean)false);
    89. };
    90.  
    91. 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) . ').');
    92. 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) . ').');
    93. endif;
    94.  
    95. $this->_Prv_Int_ConnectIdentity ? $this->_Connect= true : $this->_Connect= false;
    96. return((boolean)$this->_Connect);
    97. }
    98.  
    99. final public function _Close()
    100. {
    101. @$this->_Free_Result() ? $this->_Free_Result= true : $this->_Free_Result= false;
    102. $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) . ').');
    103. $this->_Prv_Int_CloseIdentity ? $this->_Close= true : $this->_Close= false;
    104. return((boolean)$this->_Close);
    105. }
    106.  
    107. final public function _Select()
    108. {
    109.  
    110. if(!$this->_Initialize || !$this->_Connect || $this->_Close)
    111. {
    112. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Not initialized/Not connected.');
    113. return((boolean)false);
    114. };
    115.  
    116. $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) . ').');
    117. $this->_Prv_Int_SelectIdentity ? $this->_Select= true : $this->_Select= false;
    118.  
    119. if($this->_Select)
    120. {
    121. @$this->_Free_Result();
    122. $this->_Free_Result= true;
    123. };
    124.  
    125. return((boolean)$this->_Select);
    126. }
    127.  
    128. final public function _Excute_Query($Str_Query, $Bln_RemoveTag= false, $Bln_Unbuffered= false)
    129. {
    130.  
    131. if(!is_string($Str_Query) || $this->_Is_Blank($Str_Query) || !$this->_Is_Ready())
    132. {
    133. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Invalid query/Is not ready.');
    134. return((boolean)false);
    135. };
    136.  
    137. @$this->_Free_Result();
    138. $this->_Free_Result= true;
    139. $Str_Query= @strval($Str_Query);
    140. #$Str_Query= @$this->_Clean_Value($Str_Query);
    141. if($Bln_RemoveTag): @$this->_Remove_Tag($Str_Query);
    142. endif;
    143.  
    144. 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) . ').');
    145. 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) . ').');
    146. endif;
    147.  
    148. $this->_Prv_Int_QueryIdentity ? $this->_Pub_Bln_QueryExcuted= true : $this->_Pub_Bln_QueryExcuted= false;
    149. return($this->_Pub_Bln_QueryExcuted && $this->_Pub_Bln_RowsAffected) ? (boolean)true : (boolean)false;
    150. }
    151.  
    152. final public function _Affect_Rows()
    153. {
    154.  
    155. if(!$this->_Is_Ready())
    156. {
    157. $this->_Pub_Ary_Event= array('Type'=> 'Warning', 'Description'=> 'Is not ready.');
    158. return((boolean)false);
    159. };
    160.  
    161. $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) . ').');
    162. $this->_Prv_Int_AffectedRowsIdentity ? $this->_Pub_Bln_RowsAffected= true : $this->_Pub_Bln_RowsAffected= false;
    163. return((boolean)$this->_Pub_Bln_RowsAffected);
    164. }
    165.  
    166. final public function _Free_Result()
    167. {
    168. @mysql_free_result($this->_Prv_Int_QueryIdentity) ? $this->_Free_Result= true : $this->_Free_Result= false;
    169. if(!$this->_Free_Result): $this->_Pub_Ary_Event= array('Type'=> 'Error', 'Description'=> 'Unable to free query result.');
    170. endif;
    171. return((boolean)$this->_Free_Result);
    172. }
    173.  
    174. final private function _Clean_Table_Name($Str_Input)
    175. {
    176. if(!is_string($Str_Input) || $this->_Is_Blank($Str_Input)): return((boolean)false);
    177. endif;
    178. get_magic_quotes_gpc() ? $Str_Input= @stripslashes($Str_Input) : $Str_Input= @addslashes($Str_Input);
    179.  
    180. if(function_exists('mysql_real_escape_string')): @mysql_real_escape_string($Str_Input, $this->Int_QueryIdentity);
    181. else: @mysql_escape_string($Str_Input);
    182. endif;
    183.  
    184. return((string)@htmlspecialchars($Str_Input));
    185. }
    186.  
    187. final private function _Clean_Value($Str_Input)
    188. {
    189. get_magic_quotes_gpc() ? $Str_Input= @stripslashes($Str_Input) : $Str_Input= @addslashes($Str_Input);
    190.  
    191. if(function_exists('mysql_real_escape_string')): @mysql_real_escape_string($Str_Input, $this->Int_QueryIdentity);
    192. else: @mysql_escape_string($Str_Input);
    193. endif;
    194.  
    195. return((string)$Str_Input);
    196. }
    197.  
    198. final private function _Is_Valid_Table_Prefix($Str_Input)
    199. {
    200. return(preg_match('|[^a-zA-Z0-9_]|i', $Str_Input) ? (boolean)true : (boolean)false);
    201. }
    202.  
    203. final private function _Is_Ready()
    204. {
    205. return($this->_Initialize && $this->_Connect && $this->_Prv_Int_ConnectIdentity && !$this->_Close && $this->_Select) ? (boolean)true : (boolean)false;
    206. }
    207.  
    208. final private function _Throw_Event()
    209. {
    210. }
    211.  
    212. final private function _Is_Blank($Str_Input)
    213. {
    214. return(empty($Str_Input) || is_null($Str_Input) || strlen((string)$Str_Input) <= 0) ? (boolean)true : (boolean)false;
    215. }
    216. };
    217. ?>

  7. #7

    Thread Starter
    Lively Member Y.P.Y's Avatar
    Join Date
    Sep 2008
    Location
    Tehran - Iran
    Posts
    88

    Re: New MySQL class - Give suggests to improve this

    Penagate, i dont know PDO. .
    I_Love_My_Vans, this class is not for small/simple projects. This is for long/heavy projects.

  8. #8
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: New MySQL class - Give suggests to improve this

    Quote Originally Posted by Y.P.Y
    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.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: New MySQL class - Give suggests to improve this

    Quote Originally Posted by Y.P.Y
    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: New MySQL class - Give suggests to improve this

    thanks for expanding on my question.
    My usual boring signature: Something

  11. #11
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: New MySQL class - Give suggests to improve this

    There really is no need to create your own MySQL class, instead of that use the built in MySQLi class. 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:

    PHP Code:
    $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);

    Chris

  12. #12
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: New MySQL class - Give suggests to improve this

    Quote Originally Posted by penagate
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width