Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db{
- var $Connection = array(
- 'Host' => 'localhost' , // Host of Connection.
- 'User' => 'root' , // Database's User
- 'Password' => 'toor' , // User Password
- 'Database' => 'higienizacao' , // Default Database
- 'Db_Type' => 'mysql' , // Type of Database. (Can be: Mysql , Mssql , SQLserv , PGsql).
- );
- var $Query = '' ; // Do not change.
- var $MySQLi = '' ; // Do not change.
- var $DBConnection = '' ; // Do not change.
- public function __construct($NewDatabase = '', $DebugMode = false, $SetDiferentConn = ''){
- /* About:
- -> NewDatabase: set a new database to connect (like: users or topics).
- -> DebugMode: Mode to debug your application, it shows everything that is done.
- -> SetDiferentConn: Set another connection, must to pass the initial array model ($Connection).
- */
- $this->Generalinfo = array(
- 'DebugMode' => $DebugMode ,
- 'Started_Time' => date('Y-m-d H:i:s') ,
- );
- if( ! empty( $NewDatabase ) )
- $this->Connection['Database'] = $NewDatabase;
- if( ! empty( $SetDiferentConn ) )
- $this->Connection = $SetDiferentConn;
- $this->Connection['Db_Type'] = strtolower($this->Connection['Db_Type']);
- $this->Connect();
- }
- public function __destruct(){
- /* About:
- -> Close the connection and returns the elapsed time.
- */
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->ErrorHandling();
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $this->MySQLi->close();
- break;
- case 'mssql':
- mssql_close($this->DBConnection);
- break;
- case 'sqlserv':
- sqlsrv_close($this->DBConnection);
- break;
- case 'pgsql':
- pg_close($this->DBConnection);
- break;
- }
- return $this->ElapsedTime();
- }
- public function Query($Query){
- /* About:
- -> Query($Query): Do a SQL Query. (Returns the SQL Query in a String).
- */
- $this->SQL = $Query;
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport($Query);
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $this->Query = $this->MySQLi->query($Query)
- or trigger_error(
- $this->PrintReport($Query . ' - ' . $this->MySQLi->error),
- E_USER_ERROR
- );
- break;
- case 'mssql':
- $this->Query = mssql_query(
- $Query,
- $this->DBConnection
- )
- or trigger_error(
- $this->PrintReport(mssql_get_last_message()),
- E_USER_ERROR
- );
- break;
- case 'pgsql':
- $this->Query = pg_query(
- $Query,
- $this->DBConnection
- ) or trigger_error(
- $this->PrintReport(pg_last_error($this->DBConnection)),
- E_USER_ERROR
- );
- break;
- case 'sqlserv':
- $this->Query = sqlsrv_query(
- $this->DBConnection,
- $Query,
- array(),
- array(
- "Scrollable" => "buffered"
- )
- ) or trigger_error(
- $this->PrintReport(print_r(sqlsrv_errors())),
- E_USER_ERROR
- );
- break;
- }
- return $this->Query;
- }
- public function Fetch($Query = ''){
- /* About:
- -> Fetch($Query): Fetch rows of the SQL Query. (If no rows: returns the SQL Query in a String).
- */
- if ( ! empty( $Query ) )
- $this->Query($this->SQL);
- $Query = $this->Query;
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- if(is_object($this->Query))
- return $this->Query->fetch_array(MYSQLI_ASSOC);
- break;
- case 'mssql':
- echo 'fetching..';
- return mssql_fetch_row($Query);
- break;
- case 'pgsql':
- return pg_fetch_row($Query);
- break;
- case 'sqlserv':
- return sqlsrv_fetch_array($Query, SQLSRV_FETCH_ASSOC);
- break;
- }
- }
- public function NumRows($Query = ''){
- /* About:
- -> NumRows($Query): Returns the number of rows in a SQL Query.
- */
- if ( ! empty( $Query ) )
- $this->Query($this->SQL);
- $RowsNumber = 0;
- $Query = $this->Query;
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $RowsNumber = $this->Query->num_rows;
- break;
- case 'mssql':
- $RowsNumber = mssql_num_rows($Query);
- break;
- case 'pgsql':
- $RowsNumber = pg_num_rows($Query);
- break;
- case 'sqlserv':
- $RowsNumber = sqlsrv_num_rows($Query);
- break;
- }
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport(' '.$RowsNumber.' Lines Found');
- return $RowsNumber;
- }
- public function HaveRows($Query=''){
- /* About:
- -> NumRows($Query): Returns if there are rows in SQL Query.
- */
- if ( ! empty( $Query ) )
- $this->Query($this->SQL);
- $Query = $this->Query;
- if ($this->NumRows($Query) > 0){
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport('Have Rows: TRUE');
- return true;
- }else{
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport('Have Rows: FALSE');
- return false;
- }
- }
- public function AffectedRows(){
- /* About:
- -> AffectedRows(): Returns the number of affected rows in a SQL Query.
- */
- $RowsNumber = 0;
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $RowsNumber = $this->MySQLi->affected_rows;
- break;
- case 'mssql':
- $RowsNumber = mssql_rows_affected($this->DBConnection);
- break;
- case 'pgsql':
- $RowsNumber = pg_affected_rows($this->Query);
- break;
- case 'sqlserv':
- $RowsNumber = sqlsrv_rows_affected($this->Query);
- break;
- }
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport('Rows Affected: '.$RowsNumber);
- return $RowsNumber;
- }
- public function QueryResult(){
- /* About:
- -> QueryResult(): Returns true if there aren't errors and false if there are.
- */
- if ($this->AffectedRows() >= 0 )
- $resultado = true;
- if ($this->NumRows() >= 0)
- $resultado = true;
- if ( !empty( $resultado ) ){
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport('Query Result: NO ERRORS');
- return true;
- }else{
- if ( ! empty( $this->Generalinfo['DebugMode'] ) )
- $this->PrintReport('Query Result: ERRORS IN EXECUTION');
- return false;
- }
- }
- public function ElapsedTime(){
- /* About:
- -> ElapsedTime(): Returns the elapsed time of the object execution.
- */
- $TimeNow = date('Y-m-d H:i:s');
- return (strtotime($TimeNow) - strtotime($this->Generalinfo['Started_Time']));
- }
- public function Close(){
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $this->MySQLi->close();
- break;
- case 'mssql':
- mssql_close($this->DBConnection);
- break;
- case 'sqlserv':
- sqlsrv_close($this->DBConnection);
- break;
- case 'pgsql':
- pg_close($this->DBConnection);
- break;
- }
- }
- // Support Functions
- private function PrintReport($String){
- return '<p><b>T-Rex Engine Report:</b> '. $String . '</p>';
- }
- private function Connect(){
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- $this->MySQLi = new MySQLi(
- $this->Connection['Host'] ,
- $this->Connection['User'] ,
- $this->Connection['Password'] ,
- $this->Connection['Database']
- )
- or trigger_error(
- $this->PrintReport(MySQLi_connect_error()),
- E_USER_ERROR
- );
- break;
- case 'mssql':
- $this->DBConnection = mssql_connect(
- $this->Connection['Host'] ,
- $this->Connection['User'] ,
- $this->Connection['Password']
- )
- or trigger_error(
- $this->PrintReport(mssql_get_last_message()),
- E_USER_ERROR
- );
- mssql_select_db($this->Connection['Database'], $this->DBConnection);
- break;
- case 'sqlserv':
- $this->DBConnection = sqlsrv_connect(
- $this->Connection['Host'],
- array(
- "Database" => $this->Connection['Database'] ,
- 'UID' => $this->Connection['User'] ,
- 'PWD' => $this->Connection['Password']
- )
- )
- or trigger_error(
- $this->PrintReport( print_r(sqlsrv_errors()) ),
- E_USER_ERROR
- );
- break;
- case 'pgsql':
- $this->DBConnection = pg_connect(
- "host=" . $this->Connection['Host'] .
- "dbname=" . $this->Connection['Database'] .
- "user=" . $this->Connection['User'] .
- "password=" . $this->Connection['Password']
- )
- or trigger_error(
- $this->PrintReport(pg_last_error()),
- E_USER_ERROR
- );
- break;
- }
- }
- private function ErrorHandling(){
- switch ($this->Connection['Db_Type']) {
- case 'mysql':
- if ( ! empty( $this->MySQLi->error ) )
- return trigger_error(
- $this->PrintReport($this->MySQLi->error),
- E_USER_ERROR
- );
- break;
- case 'mssql':
- if (!empty(mssql_get_last_message()))
- return trigger_error(
- $this->PrintReport(mssql_get_last_message()),
- E_USER_ERROR
- );
- break;
- case 'pgsql':
- if (!empty(pg_last_error($this->DBConnection)))
- return trigger_error(
- $this->PrintReport(pg_last_error($this->DBConnection)),
- E_USER_ERROR
- );
- break;
- case 'sqlserv':
- if (!empty(sqlsrv_errors()))
- return trigger_error(
- $this->PrintReport(print_r(sqlsrv_errors())),
- E_USER_ERROR
- );
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement