Location: PHPKode > scripts > Cyber MySQL > cbmysql.0.8.php
<?

/*
  
  Copyright (C) 2010  Nicholas

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

  If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

    <program>  Copyright (C) <year>  <name of author>
    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License.  Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".

  You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.

  The GNU General Public License does not permit incorporating your program
into proprietary programs.  If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library.  If this is what you want to do, use the GNU Lesser General
Public License instead of this License.  But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.


*/

include("conf/config.php");

include(_SQL_PATH_."lang/"._SQL_LANG_.".php");

class SQLConnect {
	
	private $data;
	private $rows;
	public  $result;
	private  $query = array();
	private $content;
	
	private  $DebugMode = false;
	
	
	
	
	
	
	public  function showError($message){
	
		printf("Error: %s",$message);
		exit();
		
	}
	
	public  function ShowDebug($msg){
		
		
		printf("DEBUG: %s", $msg);
		
	}
	
	public function AllowDebug($bool){
		
		
		$this->DebugMode = (bool) $bool;
		
	}
	
	private  function RunQuery($query){
		
		if(!empty($query)){
		$this->result = mysql_query($query) or die(_QUERY_FAILED_." ".mysql_error());
		
			if($this->DebugMode == true){
			
			die($query);
				
			}
			
			
			return true;
			
		}else{
			
			$this->showError(_QUERY_EMPTY_);
			return false;
			
		}
		
		
	}
	
	
	
	
	
	public  function Query($sql, $debugmode = false){
				
				if(strlen($sql) > 0){
				
				$this->AllowDebug($debugmode);
				$this->RunQuery($sql);
				$this->data = mysql_fetch_assoc($this->result);
				$this->rows = mysql_num_rows($this->result);
				//SQLConnect::$data = mysql_fetch_assoc(SQLConnect::$result);
				//SQLConnect::$rows = mysql_fetch_row(SQLConnect::$result);
				
				
				if($this->DebugMode == true){
					
					$this->ShowDebug($sql);
					
				}
				
				return $this->data;
			
			}else{
				
				$this->showError(_QUERY_EMPTY_);
				
			}	
	} // Fim Function Query
	
	public function __construct($query = NULL){
		
		if(!empty($query)  || $query != NULL ){
			
			$this->Query($query);
		}
		
		return $this->data;
		
	}
	
	public function CloseSQL(){
		
		 mysql_close($this->result);
				
	}
	
	public function SQLInsertQuery($query){
		
		$parse = sprintf("%s", $query);
		
		
		$this->RunQuery($parse);
		
		
	}
	
	
	public function SQLInsert($table, $fields, $content, $isAssoc = false, $debug = false){
		
		$countFields = count($fields);
		$countContent = count($content);
		
		if($countFields == 0 || $countContent == 0){
				printf(_COUNT_FIELDS_FAILED_ , $countFields, $countContent);
				exit();
				
		}else{  
			if(empty($table) || empty($fields) || empty($content) ){
				
				SQLConnect::showError(_MULTI_INSERT_ERROR_);
					
			}
		
		}
		
		if( $isAssoc == true){  // se eh matriz associativa
		
			foreach($content as $conteudo => $num){
				
				
				$assoc_cont[$conteudo] = '\''.$num.'\'';

			}
			
			
			foreach($fields as $campos => $key){
				
				 $assoc_campos[$campos] = $key;
				
			
				
				}
			
			
				$fields_final = implode(',', $assoc_campos);
				$cont_final = implode(',',  $assoc_cont);
				
				$clean_fields = substr($fields_final,0, strlen($fields_final));
				$clean_cont = substr($cont_final,0, strlen($cont_final) - 3);
				
				
			
			
		}else{
			
			
			for($i = 0; $i <= $countContent; $i++){
				
				
				
				$aCont[$i] = "'".stripslashes(mysql_real_escape_string($content[$i]))."'";
				$aFields[$i] = stripslashes(mysql_real_escape_string($fields[$i]));
				
		
				
			}
			
			
			$fields_final  =  implode(',', $aFields);
			$cont_final = implode(',', $aCont);
			
			
			$clean_comma_field = substr($fields_final, 0 ,  strlen($fields_final) - 1);
			$clean_comma_cont = substr($cont_final, 0,  strlen($cont_final) - 3);
			
		}
		
			
			
			
			
			
			
		/*	
				$final_fields = implode(',', $aFields);
			$lastword = substr($final_fields, strlen($finald_fields),  strlen($final_fields) - 1);
			
			$final_content = implode(',',$aCont);*/
			
			if($isAssoc == true){
				
				 $qstring = sprintf("INSERT INTO %s (%s) VALUES(%s)", $table, $clean_fields, $clean_cont);
				
			}else{
			 
			 $qstring = sprintf("INSERT INTO %s (%s) VALUES(%s)", $table, $clean_comma_field, $clean_comma_cont);
			 
			}
			
			
			
			//$this->ShowDebug($qstring);
			
			
			
			
			
		
			return $this->RunQuery($qstring);
	
			
			
			
	}
	

	
	public function SQLUpdate($table, $oldField, $newField, $fieldToCompare, $valueToCompare, $debug = false){
		
		if($debug == true){
			
				die($qstring);
				
			}
		
		if(empty($table) || empty($oldField) || empty($newField) || empty($fieldToCompare) || empty($valueToCompare) ){
					
			$this->showError(_UPDATE_EMPTY_FIELDS_);
		}else{
			
			$qstring = sprintf("UPDATE %s SET %s = '%s' WHERE %s = '%s' ", $table, $oldField, $newField, $fieldToCompare, $valueToCompare);
			
			if($debug == true){
			
				die($qstring);
				
			}
			
			$this->RunQuery($qstring);
			
		}
		
	}
	
	public function SQLDelete($table, $field, $value){
		
		if(empty($table) || empty($field) || empty($value)){
			
				$this->showError(_DELETE_EMPTY_FIELDS_);
		}else{
			
			$qstring = sprintf("DELETE FROM %s WHERE %s = '%s'", $table, $field, $value);
			
			
			
			
			$this->RunQuery($qstring);
			
		}
		
		
	}
	
	
	public function getRows(){
		
		return $this->rows;	
	}
	
	public function getContent(){
		
		return $this->data;
		
	}
	
	public function returnId(){
		
		
		return mysql_insert_id();
	}
	
	
} // Fim Classe




?>
Return current item: Cyber MySQL