Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class mysql
- {
- public $conn = "";
- public $debug = 0;
- public $queries;
- public function mysql( $dbUser = "user", $dbPass = "pass", $dbName = "database", $dbHost = "localhost" )
- {
- global $config;
- $this->user = $dbUser;
- $this->pass = $dbPass;
- $this->name = $dbName;
- $this->host = $dbHost;
- if ( $this->debug == 1 )
- {
- $this->queries = array( );
- $this->comments = array( );
- }
- $this->last_result = FALSE;
- $this->debug = $config['debug'];
- return TRUE;
- }
- public function connect( )
- {
- if ( !$this->conn = mysql_connect( $this->host, $this->user, $this->pass ) )
- {
- exit( "ERR_DB_CONNECT" );
- }
- $this->name( $this->name );
- return $this->conn;
- }
- public function select_db( $db )
- {
- if ( !mysql_select_db( $db, $this->conn ) )
- {
- exit( "ERR_MYSQL_SELECT_DB" );
- }
- $this->query( "set names utf8" );
- }
- public function query( $query, $comment = "" )
- {
- if ( $this->conn )
- {
- $this->conn = $this->connect( );
- }
- $start = microtime( );
- if ( !( $result = mysql_query( $query, $this->conn ) ) )
- {
- exit( mysql_error( ) );
- }
- $end = microtime( );
- if ( $this->debug == 1 )
- {
- # $sec1 = explode( " ", $start )[1];
- # $usec1 = explode( " ", $start )[0];
- # $sec2 = explode( " ", $end )[1];
- # $usec2 = explode( " ", $end )[0];
- $diff = round( $sec2 - $sec1 + $usec2 - $usec1, 5 );
- $this->queries[] = $query;
- $this->comments[] = $comment;
- $this->queries['time'][] = $diff;
- }
- $this->last_result = $result;
- return $result;
- }
- public function fetch_object( $res = FALSE )
- {
- $res = $res ? $res : $this->last_result;
- return mysql_fetch_object( $res );
- }
- public function fetch_array( $res = FALSE, $type = "name" )
- {
- $res = $res ? $res : $this->last_result;
- if ( $type == "name" )
- {
- return mysql_fetch_array( $res, MYSQL_ASSOC );
- }
- return mysql_fetch_array( $res, MYSQL_NUM );
- }
- public function num_rows( $res = FALSE )
- {
- $res = $res ? $res : $this->last_result;
- return mysql_num_rows( $res );
- }
- public function insert_id( )
- {
- return mysql_insert_id( );
- }
- public function affected_rows( )
- {
- return mysql_affected_rows( );
- }
- public function close( )
- {
- if ( $this->conn )
- {
- mysql_close( $this->conn );
- }
- }
- public function print_queries( )
- {
- $sumTime = 0;
- if ( is_array( $this->queries['time'] ) )
- {
- $sumTime = array_sum( $this->queries['time'] );
- }
- @$html = "<hr style=\"clear: both\"/><div style=\"margin-left: 10px;\">";
- @$html .= @"<div style=\"color: red; text-decoration: underline; margin-bottom: 5px;\">Queries: <b>".@count( @$this->queries['time'] )."</b>; queriesExecuteTime: <b>".$sumTime."</b></div>";
- if ( empty( $this->queries['time'] ) )
- {
- foreach ( $this->queries['time'] as $key => $value )
- {
- $key2 = $key + 1;
- $html .= nl2br( "#".$key2." - ".$this->queries[$key]( $this->queries[$key] ) ).";( <b><font color=yellow>".$this->comments[$key]."</b></font> ) <b>�����: <font color=blue>".sprintf( "%.5f", $this->queries['time'][$key] )."</font></b><hr>";
- }
- }
- $html .= "</div>";
- return $html;
- }
- public function getFoundRows( )
- {
- $this->query( "\n\t\t\tSELECT FOUND_ROWS() AS f_rows\n\t\t" );
- $d = $this->fetch_array( );
- return $d['f_rows'];
- }
- public function getRowCount( )
- {
- $this->query( "\n\t\t\tSELECT ROW_COUNT() AS f_rows\n\t\t" );
- $d = $this->fetch_array( );
- return $d['f_rows'];
- }
- public function queryColor( $query )
- {
- $query = preg_replace( "/['\"]([^'\"]*)['\"]/i", "'<FONT COLOR='#FF6600'>$1</FONT>'", $query, -1 );
- $query = str_ireplace( array( "*", "SELECT", "UPDATE ", "DELETE ", "INSERT ", "INTO", "VALUES", "FROM", "LEFT", "JOIN", "WHERE", "LIMIT", "ORDER BY", "GROUP BY", "AND", "OR ", "DESC", "ASC", "ON " ), array( "<FONT COLOR='#FF6600'><B>*</B></FONT>", "<FONT COLOR='#00AA00'><B>SELECT</B> </FONT>", "<FONT COLOR='#00AA00'><B>UPDATE</B> </FONT>", "<FONT COLOR='#00AA00'><B>DELETE</B> </FONT>", "<FONT COLOR='#00AA00'><B>INSERT</B> </FONT>", "<FONT COLOR='#00AA00'><B>INTO</B></FONT>", "<FONT COLOR='#00AA00'><B>VALUES</B></FONT>", "<FONT COLOR='#00AA00'><B>FROM</B></FONT>", "<FONT COLOR='#00CC00'><B>LEFT</B></FONT>", "<FONT COLOR='#00CC00'><B>JOIN</B></FONT>", "<FONT COLOR='#00AA00'><B>WHERE</B></FONT>", "<FONT COLOR='#AA0000'><B>LIMIT</B></FONT>", "<FONT COLOR='#00AA00'><B>ORDER BY</B></FONT>", "<FONT COLOR='#00AA00'><B>GROUP BY</B></FONT>", "<FONT COLOR='#0000AA'><B>AND</B></FONT>", "<FONT COLOR='#0000AA'><B>OR</B> </FONT>", "<FONT COLOR='#0000AA'><B>DESC</B></FONT>", "<FONT COLOR='#0000AA'><B>ASC</B></FONT>", "<FONT COLOR='#00DD00'><B>ON</B> </FONT>" ), $query );
- return $query;
- }
- public function getRow( $query )
- {
- $result = $this->query( $query );
- if ( mysql_num_rows( $result ) == 0 )
- {
- return FALSE;
- }
- return $this->fetch_array( );
- }
- public function getCol( $query )
- {
- $result = $this->query( $query );
- if ( mysql_num_rows( $result ) == 0 )
- {
- return FALSE;
- }
- while ( $row = mysql_fetch_array( $result ) )
- {
- $rows[] = $row[0];
- }
- return $rows;
- }
- public function getValue( $query )
- {
- $result = $this->query( $query );
- if ( mysql_num_rows( $result ) == 0 )
- {
- return FALSE;
- }
- $row = mysql_fetch_array( $result );
- return $row[0];
- }
- public function insert( $tableName, $array )
- {
- if ( is_array( $array ) )
- {
- $arrayKeys = array_keys( $array );
- foreach ( $arrayKeys as $value )
- {
- if ( strtolower( $value ) == "pk" )
- {
- $arrayKeys2 = array_keys( $array[$value] );
- foreach ( $arrayKeys2 as $value2 )
- {
- $insertArray[$value2] = $array[$value][$value2];
- }
- }
- else if ( strtolower( $value ) == "iu" )
- {
- $arrayKeys2 = array_keys( $array[$value] );
- foreach ( $arrayKeys2 as $value2 )
- {
- $insertArray[$value2] = $array[$value][$value2];
- }
- }
- }
- $columnName = "";
- $columnValue = "";
- $arrayKeys = array_keys( $insertArray );
- foreach ( $arrayKeys as $value )
- {
- if ( $columnName != "" )
- {
- $columnName .= ", ";
- $columnValue .= ", ";
- }
- if ( $insertArray[$value] != "NOW()" && $insertArray[$value] != "NULL" )
- {
- $columnValue .= " '".sql_quote( $insertArray[$value] )."' ";
- }
- else
- {
- $columnValue .= " ".$insertArray[$value]." ";
- }
- $columnName .= " `".sql_quote( $value )."` ";
- }
- $this->query( "INSERT INTO `".$tableName."`({$columnName}) VALUES({$columnValue})" );
- return TRUE;
- }
- return FALSE;
- }
- public function update( $tableName, $array )
- {
- if ( is_array( $array ) )
- {
- $setQuery = "";
- $whereQuery = "";
- $arrayKeys = array_keys( $array );
- foreach ( $arrayKeys as $value )
- {
- if ( strtolower( $value ) == "pk" )
- {
- $arrayKeys2 = array_keys( $array[$value] );
- foreach ( $arrayKeys2 as $value2 )
- {
- if ( $whereQuery == "" )
- {
- $whereQuery = "`".$value2."` = '".$array[$value][$value2]."'";
- }
- else
- {
- $whereQuery .= " AND `".$value2."` = '".$array[$value][$value2]."'";
- }
- }
- }
- else if ( strtolower( $value ) == "iu" )
- {
- $arrayKeys2 = array_keys( $array[$value] );
- foreach ( $arrayKeys2 as $value2 )
- {
- if ( $array[$value][$value2] == "NULL" || $array[$value][$value2] == "NOW()" )
- {
- $updateValue = sql_quote( $array[$value][$value2] );
- }
- else
- {
- $updateValue = "'".sql_quote( $array[$value][$value2] )."'";
- }
- if ( $setQuery == "" )
- {
- $setQuery = "`".$value2."` = {$updateValue}";
- }
- else
- {
- $setQuery .= ", `".$value2."` = {$updateValue}";
- }
- }
- }
- }
- $this->query( "UPDATE `".$tableName."` SET {$setQuery} WHERE {$whereQuery}" );
- return TRUE;
- }
- return FALSE;
- }
- public function getAll( $query, $field = "" )
- {
- $this->query( $query, "getAll" );
- if ( $this->num_rows( ) < 1 )
- {
- return FALSE;
- }
- $return = array( );
- while ( $d = $this->fetch_array( ) )
- {
- if ( $field == "" )
- {
- $return[] = $d;
- }
- else
- {
- $return[$d[$field]] = $d;
- }
- }
- return $return;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment