Guest User

Untitled

a guest
Apr 26th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2. class MySQL{
  3. /**
  4. * Vars
  5. *
  6. * Set up all the vars we need with some basic default vars
  7. **/
  8. private $sqlConnection;
  9. private $errorMode = TRUE;
  10. private $debugMode = FALSE;
  11. private $errorLocation = '/home/user/error/';
  12. private $errorFile = 'MySQL.txt';
  13.  
  14. protected $sqlUser = 'root';
  15. protected $sqlPass = '';
  16. protected $sqlDB = 'root';
  17. protected $sqlServer = 'localhost';
  18. protected $sqlPort = 3306;
  19.  
  20. public $lastQuery;
  21. public $errors = 0;
  22. private $queries = 0;
  23.  
  24. /**
  25. * __construct
  26. *
  27. * Connect to the database in here and do other important things.
  28. *
  29. * @params config An array containing all details needed for the class
  30. **/
  31.  
  32. function __construct( $config = array( ) ){
  33. if( is_array( $config ) ){
  34. foreach( $config as $key => $val ){
  35. $this->$key = $val;
  36. }
  37. }
  38. else{
  39. //errors
  40. }
  41.  
  42. $this->sqlConnect( );
  43. }
  44.  
  45. /**
  46. * sqlConnect
  47. *
  48. * Connects to the mysql server then selects the databases
  49. **/
  50. function sqlConnect( ){
  51. if( $this->sqlConnection = @mysql_connect( $this->sqlServer:$this->sqlPort, $this->sqlUser, $this->sqlPass ) ){
  52. if( @mysql_select_db( $this->sqlDB, $this->connection ){
  53. return true;
  54. }
  55. else{
  56. $this->sqlError( 'Error selecting database: '.$this->sqlDB );
  57. }
  58.  
  59. }
  60. else{
  61. $this->sqlError( 'Error Occured while trying to connect to MySQL Server: '.$this->sqlServer.' on Port: '.$this->sqlPort );
  62. }
  63. }
  64.  
  65. /**
  66. * sqlDisconnect
  67. *
  68. * Disconnect from the mysql server.
  69. **/
  70. function sqlDisconnect( ){
  71. if( $this->sqlConnection ){
  72. mysql_close( $this->sqlConnection );
  73. }
  74. else{
  75. $this->sqlError( "Trying to disconnect from mysql. No mysql connection found" );
  76. }
  77. }
  78.  
  79. /**
  80. * sqlError
  81. *
  82. * Stores all mysql errors in a text file, returns them if errors are on
  83. * and add's to the error counter
  84. *
  85. * @params message Message that should be returned
  86. **/
  87. function sqlError( $message ){
  88. $this->errors++;
  89. if( $this->errorMode == TRUE ){
  90. echo $message;
  91. $this->logError( $message );
  92. }
  93. else{
  94. $this->logError( $message );
  95. }
  96. }
  97.  
  98. /**
  99. * logError
  100. *
  101. * Stores the error into the text file. If file is not writable then
  102. * we try to chmod it.
  103. *
  104. * @params message The message which will be stored in the text file
  105. **/
  106. function logError( $message ){
  107. $handle = fopen( $this->errorLocation.$this->errorFile, 'a+' );
  108. $data = "\n [".time( )."] - ".$message."\n";
  109. if( is_writable( $this->errorLocation.$this->errorFile ) ){
  110. if( fwrite( $handle, $data ) ){
  111. return true;
  112. }
  113. else{
  114. echo 'Fatal Error: File could not be written to. Unknown Error.';
  115. }
  116. }
  117. else{
  118. if( chmod( $this->errorLocation.$this->errorFile, 777 ) ){
  119. $this->logError( $message );
  120. }
  121. else{
  122. echo 'Fatal Error: Error file cannot be written to or chmodded.';
  123. }
  124. }
  125. }
  126.  
  127. /**
  128. * sqlResponse
  129. *
  130. * Returns the response time of the mysql server
  131. **/
  132. function sqlResponse( ){
  133. $ts = microtime( true );
  134. $check = fsockopen( gethostbyname( $this->sqlServer ), $this->sqlPort );
  135. if( $check ){
  136. echo 'MySQL Server is up and running... Response time: '.chr( 10 ).chr( 13 ).( microtime( true ) - $ts ) . ' seconds.';
  137. }
  138. else{
  139. $this->sqlError( 'MySQL Server is not responding' );
  140. }
  141. }
  142. }
  143.  
  144. $config = array("debugMode" => TRUE,
  145. "sqlUser" => "cssfrenz_test",
  146. "sqlPass" => "xxxxxxxxxxxxxx",
  147. "sqlDB" => "cssfrenz_test",
  148. "sqlPort" => 3306,
  149.  
  150. "errorLocation" => "/home/cssfrenz/public_html/sandbox/error",
  151. "errorFile" => "mysql.txt");
  152.  
  153. $mysql = new mysql( );
  154. ?>
Add Comment
Please, Sign In to add comment