Added mysql class
This commit is contained in:
parent
6538924688
commit
80c27ce1ee
2 changed files with 192 additions and 14 deletions
178
src/Databases/MySQLClass.php
Normal file
178
src/Databases/MySQLClass.php
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
<?php
|
||||
|
||||
namespace PhangoApp\PhaModels;
|
||||
|
||||
if(!function_exists('mysql_query'))
|
||||
{
|
||||
|
||||
throw new \Exception('Error: Mysql database don\'t supported by php');
|
||||
|
||||
}
|
||||
|
||||
class MySQLClass {
|
||||
|
||||
static public $debug=true;
|
||||
|
||||
static public function print_sql_fail($sql_fail, $server_data='default')
|
||||
{
|
||||
|
||||
$error=mysqli_error(Webmodel::$connection[$server_data]);
|
||||
|
||||
if($error!='' && MySQLClass::$debug==true)
|
||||
{
|
||||
//echo '<p>Error: '.$sql_fail.' -> '.$error.'</p>';
|
||||
throw new \Exception('Error: '.$sql_fail.' -> '.$error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static public function webtsys_query( $sql_string , $server_data='default')
|
||||
{
|
||||
|
||||
|
||||
$query = mysqli_query(Webmodel::$connection[$server_data], $sql_string );
|
||||
|
||||
MySQLClass::print_sql_fail($sql_string, $server_data);
|
||||
|
||||
Webmodel::$save_query++;
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
static public function webtsys_affected_rows( $idconnection , $server_data='default')
|
||||
{
|
||||
|
||||
$num_rows = mysqli_affected_rows(Webmodel::$connection[$server_data], $idconnection );
|
||||
|
||||
return $num_rows;
|
||||
}
|
||||
|
||||
static public function webtsys_close( $idconnection )
|
||||
{
|
||||
|
||||
mysqli_close( $idconnection );
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static public function webtsys_fetch_array( $query ,$assoc_type=0)
|
||||
{
|
||||
|
||||
$arr_assoc[0]=MYSQL_ASSOC;
|
||||
$arr_assoc[1]=MYSQL_NUM;
|
||||
|
||||
$arr_final = mysqli_fetch_array( $query ,$arr_assoc[$assoc_type]);
|
||||
|
||||
return $arr_final;
|
||||
}
|
||||
|
||||
static public function webtsys_fetch_row( $query )
|
||||
{
|
||||
$arr_final = mysqli_fetch_row( $query );
|
||||
|
||||
return $arr_final;
|
||||
}
|
||||
|
||||
static public function webtsys_get_client_info($server_data='default')
|
||||
{
|
||||
|
||||
$version = mysqli_get_client_info(Webmodel::$connection[$server_data]);
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
static public function webtsys_get_server_info($server_data='default')
|
||||
{
|
||||
|
||||
$version = mysqli_get_server_info(Webmodel::$connection[$server_data]);
|
||||
|
||||
return $version;
|
||||
}
|
||||
|
||||
static public function webtsys_insert_id($server_data='default')
|
||||
{
|
||||
|
||||
$idinsert = mysqli_insert_id(Webmodel::$connection[$server_data]);
|
||||
|
||||
return $idinsert;
|
||||
}
|
||||
|
||||
static public function webtsys_num_rows( $query )
|
||||
{
|
||||
$num_rows = mysqli_num_rows( $query );
|
||||
|
||||
return $num_rows;
|
||||
}
|
||||
|
||||
/*static public function connection_database( $host_db, $login_db, $contra_db, $db )
|
||||
{
|
||||
global $con_persistente;
|
||||
|
||||
Webmodel::$connection = $con_persistente( $host_db, $login_db, $contra_db );
|
||||
|
||||
webtsys_select_db( $db );
|
||||
|
||||
return Webmodel::$connection;
|
||||
}*/
|
||||
|
||||
static public function webtsys_connect( $host_db, $login_db, $contra_db , $server_data='default')
|
||||
{
|
||||
|
||||
Webmodel::$connection[$server_data]=mysqli_init();
|
||||
|
||||
if ( !( mysqli_real_connect(Webmodel::$connection[$server_data], $host_db, $login_db, $contra_db ) ) )
|
||||
{
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
//return Webmodel::$connection;
|
||||
}
|
||||
|
||||
static public function webtsys_pconnect( $host_db, $login_db, $contra_db, $server_data='default' )
|
||||
{
|
||||
|
||||
if ( !( Webmodel::$connection[$server_data] = @mysql_pconnect( $host_db, $login_db, $contra_db ) ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Webmodel::$connection;
|
||||
}
|
||||
|
||||
static public function webtsys_select_db( $db , $server_data='default')
|
||||
{
|
||||
|
||||
$result_db=mysqli_select_db(Webmodel::$connection[$server_data], $db);
|
||||
|
||||
if($result_db==false)
|
||||
{
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static public function webtsys_escape_string($sql_string, $server_data='default')
|
||||
{
|
||||
|
||||
return mysqli_real_escape_string(Webmodel::$connection[$server_data], $sql_string);
|
||||
|
||||
}
|
||||
|
||||
static public function webtsys_error($server_data='default')
|
||||
{
|
||||
|
||||
return mysqli_error(Webmodel::$connection[$server_data]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
@ -37,7 +37,7 @@ class Webmodel {
|
|||
|
||||
static public $pass_db=array();
|
||||
|
||||
static public $type_db='mysql';
|
||||
static public $type_db='MySQLClass';
|
||||
|
||||
/**
|
||||
* Connection to db.
|
||||
|
|
@ -300,10 +300,10 @@ class Webmodel {
|
|||
public function connect_to_db()
|
||||
{
|
||||
|
||||
include(__DIR__.'/database/'.Webmodel::$type_db.'.php');
|
||||
include(__DIR__.'/Databases/'.Webmodel::$type_db.'.php');
|
||||
//load_libraries(array('database/'.Webmodel::$type_db), Webmodel::$base_path);
|
||||
|
||||
if(!webtsys_connect( Webmodel::$host_db[$this->db_selected], Webmodel::$login_db[$this->db_selected], Webmodel::$pass_db[$this->db_selected] , $this->db_selected))
|
||||
if(!MySQLClass::webtsys_connect( Webmodel::$host_db[$this->db_selected], Webmodel::$login_db[$this->db_selected], Webmodel::$pass_db[$this->db_selected] , $this->db_selected))
|
||||
{
|
||||
|
||||
$output=ob_get_contents();
|
||||
|
|
@ -321,7 +321,7 @@ class Webmodel {
|
|||
|
||||
}
|
||||
|
||||
Webmodel::$select_db[$this->db_selected]=webtsys_select_db( Webmodel::$db[$this->db_selected] , $this->db_selected);
|
||||
Webmodel::$select_db[$this->db_selected]=MySQLClass::webtsys_select_db( Webmodel::$db[$this->db_selected] , $this->db_selected);
|
||||
|
||||
if(Webmodel::$select_db[$this->db_selected]!=false && Webmodel::$connection[$this->db_selected]!=false)
|
||||
{
|
||||
|
|
@ -488,7 +488,7 @@ class Webmodel {
|
|||
if( $fields=$this->check_all($post) )
|
||||
{
|
||||
|
||||
if( !( $query=webtsys_query($this->prepare_insert_sql($fields), $this->db_selected) ) )
|
||||
if( !( $query=MySQLClass::webtsys_query($this->prepare_insert_sql($fields), $this->db_selected) ) )
|
||||
{
|
||||
|
||||
$this->std_error.=Webmodel::$l_['error_model']->lang('cant_insert', 'Can\'t insert').' ';
|
||||
|
|
@ -604,7 +604,7 @@ class Webmodel {
|
|||
|
||||
//Create the query..
|
||||
|
||||
if(!($query=webtsys_query('update '.$this->name.' set '.implode(', ' , $arr_fields).' '.$conditions, $this->db_selected) ) )
|
||||
if(!($query=MySQLClass::webtsys_query('update '.$this->name.' set '.implode(', ' , $arr_fields).' '.$conditions, $this->db_selected) ) )
|
||||
{
|
||||
|
||||
$this->std_error.=Webmodel::$l_['error_model']->lang('cant_update', 'Can\'t update').' ';
|
||||
|
|
@ -631,7 +631,7 @@ class Webmodel {
|
|||
}
|
||||
|
||||
//This method select a row in database using model data
|
||||
//You have use webtsys_fetch_row or alternatives for obtain data
|
||||
//You have use MySQLClass::webtsys_fetch_row or alternatives for obtain data
|
||||
//Conditions are sql lang, more simple, more kiss
|
||||
|
||||
/**
|
||||
|
|
@ -788,7 +788,7 @@ class Webmodel {
|
|||
$arr_distinct[0]='';
|
||||
$arr_distinct[1]=' DISTINCT ';
|
||||
|
||||
$query=webtsys_query('select '.$arr_distinct[$this->distinct].' '.$fields.' from '.$selected_models.' '.$conditions, $this->db_selected);
|
||||
$query=MySQLClass::webtsys_query('select '.$arr_distinct[$this->distinct].' '.$fields.' from '.$selected_models.' '.$conditions, $this->db_selected);
|
||||
|
||||
$this->distinct=0;
|
||||
|
||||
|
|
@ -883,9 +883,9 @@ class Webmodel {
|
|||
|
||||
}
|
||||
|
||||
$query=webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected);
|
||||
$query=MySQLClass::webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected);
|
||||
|
||||
list($count_field)= webtsys_fetch_row($query);
|
||||
list($count_field)= MySQLClass::webtsys_fetch_row($query);
|
||||
|
||||
return $count_field;
|
||||
|
||||
|
|
@ -942,7 +942,7 @@ class Webmodel {
|
|||
|
||||
}
|
||||
|
||||
return webtsys_query('delete from '.$this->name.' '.$conditions, $this->db_selected);
|
||||
return MySQLClass::webtsys_query('delete from '.$this->name.' '.$conditions, $this->db_selected);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -957,7 +957,7 @@ class Webmodel {
|
|||
|
||||
$this->set_phango_connection();
|
||||
|
||||
return webtsys_fetch_row($query);
|
||||
return MySQLClass::webtsys_fetch_row($query);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -972,7 +972,7 @@ class Webmodel {
|
|||
|
||||
$this->set_phango_connection();
|
||||
|
||||
return webtsys_fetch_array($query);
|
||||
return MySQLClass::webtsys_fetch_array($query);
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -987,7 +987,7 @@ class Webmodel {
|
|||
|
||||
$this->set_phango_connection();
|
||||
|
||||
return webtsys_insert_id();
|
||||
return MySQLClass::webtsys_insert_id();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue