Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Skoldat Database Mysql driver
- */
- if(!function_exists('sk_error')){
- function sk_error($str){
- die('<!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT">
- <meta http-equiv="Pragma" content="no-cache">
- <meta http-equiv="Cache-Control" content="no-cache">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <meta http-equiv="Lang" content="en">
- <meta name="author" content="">
- <meta http-equiv="Reply-to" content="@.com">
- <meta name="generator" content="PhpED 6.0">
- <meta name="description" content="">
- <meta name="keywords" content="">
- <meta name="creation-date" content="06/01/2011">
- <meta name="revisit-after" content="15 days">
- <title>Untitled</title>
- <link rel="stylesheet" type="text/css" href="my.css">
- </head>
- <body>
- <h1>'.$str.'</h1>
- </body>
- </html>');
- }
- }
- class SKDatabasemysql {
- var $connection = null;
- var $in_transaction = false;
- var $count_query = false;
- var $last_query = false;
- private $fetched_row = array();
- protected $in_params = array();
- function __construct() {
- if(!defined('SK_INT') || !defined('SK_VARCHAR') || !defined('SK_DATETIME') || !defined('SK_TIMESTAMP') || !defined('SK_DATE') || !defined('SK_TBNAME')){
- sk_error('You are trying to use an undefined constant');
- }
- $this->count_query = 0;
- }
- public function create_db($dbname){
- if($this->is_connected()){
- if(mysql_create_db($dbname,$this->connection)){
- return true;
- }
- }
- return false;
- }
- public function connect($servername=null, $connection_info = array(),$charset = 'utf8') {
- if($servername == null){
- $servername = ini_get("mysql.default_host");
- }
- if(!isset($connection_info['UID']) || $connection_info['UID'] == null){
- $connection_info['UID'] = ini_get("default_user");
- }
- if(!isset($connection_info['PWD']) || $connection_info['PWD'] == null){
- $connection_info['PWD'] = ini_get("default_password");
- }
- if(!isset($connection_info['Database']) || $connection_info['Database'] == null){
- return false;
- }
- $this->connection = mysql_connect($servername,$connection_info['UID'],$connection_info['PWD']);
- if($this->is_connected()){
- if($this->set_database($connection_info['Database'])){
- $this->exec('SET CHARACTER SET '.$this->param('charset',SK_VARCHAR),array('charset'=>$charset));
- return true;
- }
- }
- $this->disconnect();
- return false;
- }
- public function truncate_tb($table_name){
- if($this->is_connected()){
- if($table_name != null){
- $c = $this->exec('TRUNCATE TABLE '.$this->param('table_name',SK_TBNAME),array('table_name'=>$table_name));
- if($c){
- $this->close_resultset($c);
- return true;
- }
- $this->close_resultset($c);
- }
- }
- return false;
- }
- public function set_database($db_name) {
- if($this->is_connected()){
- if(mysql_select_db($db_name,$this->connection)){
- return true;
- }
- }
- return false;
- }
- public function is_connected() {
- if($this->connection == null || $this->connection === false){
- return false;
- }
- return mysql_ping($this->connection);
- }
- public function disconnect() {
- if($this->is_connected()){
- if(mysql_close($this->connection)){
- return true;
- }
- }
- return false;
- }
- public function fetch_row(&$cursor) {
- if($this->is_connected()){
- if($cursor != null){
- $this->fetched_row[$cursor] = mysql_fetch_array($cursor, MYSQL_ASSOC);
- if ($this->fetched_row[$cursor] != null) {
- $this->fetched_row[$cursor] = array_change_key_case($this->fetched_row[$cursor], CASE_UPPER);
- return true;
- }
- }
- }
- return false;
- }
- public function fetch_value($cursor, $column_name) {
- if (!$cursor) {
- return false;
- }
- if (!isset($this->fetched_row[$cursor][strtoupper($column_name)])) {
- return false;
- }
- return $this->fetched_row[$cursor][strtoupper($column_name)];
- }
- public function exec($sql_statement, $param_array = null) {
- if($param_array != null){
- foreach($this->in_params as $key => $value){
- if(isset($param_array[$value['value']])){
- if($value['type'] == SK_INT || $value['type']==SK_VARCHAR || $value['type']==SK_TBNAME){
- $sql_statement = str_replace(':'.$value['value'].':',mysql_real_escape_string($param_array[$value['value']],$this->connection),$sql_statement);
- }else{
- /**
- * You must complete the operator types: TIMESTAMP, DATE, DATETIME
- */
- }
- }
- }
- }
- $this->in_params = array();
- $this->count_query++;
- $this->last_query = $sql_statement;
- return mysql_query($sql_statement);
- }
- public function get_one_row(&$row, $sql_statement, $params = null) {
- if($sql_statement != null){
- $c = $this->exec($sql_statement,$params);
- if($this->fetch_row($c)){
- $row = $this->fetched_row[$c];
- }
- $this->close_resultset($c);
- if($row != null){
- return true;
- }
- }
- return false;
- }
- public function param($key,$type = SK_VARCHAR) {
- switch($type){
- case SK_INT:
- case SK_TBNAME:
- $param = ':'.$key.':';
- break;
- default:
- $param = '":'.$key.':"';
- break;
- }
- $count = count($this->in_params);
- $this->in_params[($count+1)]['value']=$key;
- $this->in_params[($count+1)]['type']=$type;
- return $param;
- }
- public function num_rows($cursor) {
- if($this->is_connected() && $cursor != null){
- return mysql_num_rows($cursor);
- }
- return false;
- }
- public function data_seek($cursor, $row = 0) {
- $count = $this->num_rows($cursor);
- if($this->is_connected() && $row != null && ($count >= $row) == true){
- return mysql_data_seek($cursor,$row);
- }
- else{
- return false;
- }
- }
- public function evaluate_bolean_expresion($sql_statement, $param_array = null) {
- $res = false;
- if($this->is_connected()){
- $c = $this->exec($sql_statement,$param_array);
- if($this->num_rows($c)){
- $res = true;
- }
- $this->close_resultset($c);
- }
- return $res;
- }
- public function close_resultset(&$cursor) {
- if($this->is_connected()){
- if($cursor != null){
- if(mysql_free_result($cursor)){
- if(isset($this->fetched_row[$cursor])){
- unset($this->fetched_row[$cursor]);
- }
- $cursor = null;
- return true;
- }
- }
- }
- return false;
- }
- public function drop_database($db_name){
- if($this->is_connected() && $db_name != null){
- return mysql_drop_db($db_name,$this->connection);
- }
- return false;
- }
- public function get_database_list(){
- if($this->is_connected()){
- $c = mysql_list_dbs($this->connection);
- $db_list = null;
- while ($row = mysql_fetch_object($c)) {
- $db_list[] = $row->Database;
- }
- $this->close_resultset($c);
- if($db_list != null){
- return $db_list;
- }
- }
- return false;
- }
- public function get_table_list($db_name){
- if($this->is_connected()){
- $c = $this->exec('SHOW TABLES FROM '.$db_name);
- $table_list = null;
- while ($this->fetch_row($c)) {
- $table_list[] = $this->fetched_row[$c]['TABLES_IN_'.strtoupper($db_name)];
- }
- $this->close_resultset($c);
- if($table_list != null){
- return $table_list;
- }
- }
- return false;
- }
- public function get_server_info() {
- if($this->is_connected()){
- return "MySQL server version: ".mysql_get_server_info($this->connection);
- }
- return false;
- }
- public function get_db_stats() {
- if($this->is_connected()){
- return explode(' ', mysql_stat($this->connection));
- }
- return false;
- }
- public function get_client_info() {
- if($this->is_connected()){
- return "MySQL client info: ". mysql_get_client_info();
- }
- return false;
- }
- public function get_host_info() {
- if($this->is_connected()){
- return "MySQL host info: ". mysql_get_host_info($this->connection);
- }
- return false;
- }
- public function get_affected_row(){
- if($this->is_connected()){
- return mysql_affected_rows($this->connection);
- }
- return false;
- }
- function __destruct() {
- if($this->is_connected()){
- $this->disconnect();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement