Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MySQL{
- /**
- * Vars
- *
- * Set up all the vars we need with some basic default vars
- **/
- private $sqlConnection;
- private $errorMode = TRUE;
- private $debugMode = FALSE;
- private $errorLocation = '/home/user/error/';
- private $errorFile = 'MySQL.txt';
- protected $sqlUser = 'root';
- protected $sqlPass = '';
- protected $sqlDB = 'root';
- protected $sqlServer = 'localhost';
- protected $sqlPort = 3306;
- public $lastQuery;
- public $errors = 0;
- private $queries = 0;
- /**
- * __construct
- *
- * Connect to the database in here and do other important things.
- *
- * @params config An array containing all details needed for the class
- **/
- function __construct( $config = array( ) ){
- if( is_array( $config ) ){
- foreach( $config as $key => $val ){
- $this->$key = $val;
- }
- }
- else{
- //errors
- }
- $this->sqlConnect( );
- }
- /**
- * sqlConnect
- *
- * Connects to the mysql server then selects the databases
- **/
- function sqlConnect( ){
- if( $this->sqlConnection = @mysql_connect( $this->sqlServer:$this->sqlPort, $this->sqlUser, $this->sqlPass ) ){
- if( @mysql_select_db( $this->sqlDB, $this->connection ){
- return true;
- }
- else{
- $this->sqlError( 'Error selecting database: '.$this->sqlDB );
- }
- }
- else{
- $this->sqlError( 'Error Occured while trying to connect to MySQL Server: '.$this->sqlServer.' on Port: '.$this->sqlPort );
- }
- }
- /**
- * sqlDisconnect
- *
- * Disconnect from the mysql server.
- **/
- function sqlDisconnect( ){
- if( $this->sqlConnection ){
- mysql_close( $this->sqlConnection );
- }
- else{
- $this->sqlError( "Trying to disconnect from mysql. No mysql connection found" );
- }
- }
- /**
- * sqlError
- *
- * Stores all mysql errors in a text file, returns them if errors are on
- * and add's to the error counter
- *
- * @params message Message that should be returned
- **/
- function sqlError( $message ){
- $this->errors++;
- if( $this->errorMode == TRUE ){
- echo $message;
- $this->logError( $message );
- }
- else{
- $this->logError( $message );
- }
- }
- /**
- * logError
- *
- * Stores the error into the text file. If file is not writable then
- * we try to chmod it.
- *
- * @params message The message which will be stored in the text file
- **/
- function logError( $message ){
- $handle = fopen( $this->errorLocation.$this->errorFile, 'a+' );
- $data = "\n [".time( )."] - ".$message."\n";
- if( is_writable( $this->errorLocation.$this->errorFile ) ){
- if( fwrite( $handle, $data ) ){
- return true;
- }
- else{
- echo 'Fatal Error: File could not be written to. Unknown Error.';
- }
- }
- else{
- if( chmod( $this->errorLocation.$this->errorFile, 777 ) ){
- $this->logError( $message );
- }
- else{
- echo 'Fatal Error: Error file cannot be written to or chmodded.';
- }
- }
- }
- /**
- * sqlResponse
- *
- * Returns the response time of the mysql server
- **/
- function sqlResponse( ){
- $ts = microtime( true );
- $check = fsockopen( gethostbyname( $this->sqlServer ), $this->sqlPort );
- if( $check ){
- echo 'MySQL Server is up and running... Response time: '.chr( 10 ).chr( 13 ).( microtime( true ) - $ts ) . ' seconds.';
- }
- else{
- $this->sqlError( 'MySQL Server is not responding' );
- }
- }
- }
- $config = array("debugMode" => TRUE,
- "sqlUser" => "cssfrenz_test",
- "sqlPass" => "xxxxxxxxxxxxxx",
- "sqlDB" => "cssfrenz_test",
- "sqlPort" => 3306,
- "errorLocation" => "/home/cssfrenz/public_html/sandbox/error",
- "errorFile" => "mysql.txt");
- $mysql = new mysql( );
- ?>
Add Comment
Please, Sign In to add comment