Location: PHPKode > scripts > vladzur Database > vladzur-database/database.php
<?php
/*
 *      database.php
 *      
 *      Copyright 2008 Vladimir Zurita <hide@address.com>
 *      
 *      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 2 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, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

 /********************************************************
  * DataBase
  * Author: Vladimir Zurita
  * 
  * This class makes easier to insert data into a table and connect
  * a database on MySQL
  * *******************************************************/

class DataBase {
	
	var $db=array('host'=>'localhost',
					'user'=>'your_user',
					'pass'=>'your_password',
					'dbase'=>'your_database'
					);
					
	var $link="";
	
	
	function connect(){
		
		$this->link=mysql_connect($this->db['host'], $this->db['user'], $this->db['pass']);
		mysql_select_db($this->db['dbase'], $this->link);
		
	}
	
	function close(){
		mysql_close($this->link);		
	}
	
	function query($ssql=null){
		
		$this->connect();
		if($result=mysql_query($ssql)){
			while($row=mysql_fetch_assoc($result)){
				$output[]=$row;
			}
			$this->close();
			return $output;
		}
		return false;
	}
		
	function save($tabla,$data){
		$campos=array_keys($data);
		$desc=$this->describe_tabla($tabla);
		foreach($campos as $campo){
			for($i=0;$i<count($desc);$i++){
				if($campo==$desc[$i]['Field'])$tipo=$desc[$i]['Type'];
			}
			if(stristr($tipo,"varchar"))$value.="'".$data[$campo]."', ";
			if(stristr($tipo,"text"))$value.="'".$data[$campo]."', ";
			if(stristr($tipo,"int"))$value.=$data[$campo].", ";
			if(stristr($tipo,"blob"))$value.="'".$data[$campo]."', ";
			$field.=$campo.", ";			
		}	
		$field=trim($field,", ");
		$value=trim($value,", ");	
		$sql="INSERT INTO ".$tabla." (".$field.") VALUES (".$value.")";
		//echo $sql;		
		$this->connect();
		if($result=mysql_query($sql))return true;
		$this->close();
		return false;					
	}
	
	function update($tabla,$data,$condicion){
		$campos=array_keys($data);
		$desc=$this->describe_tabla($tabla);
		foreach($campos as $campo){
			for($i=0;$i<count($desc);$i++){
				if($campo==$desc[$i]['Field'])$tipo=$desc[$i]['Type'];
			}
			if(stristr($tipo,"varchar"))$value.=$campo."='".$data[$campo]."', ";
			if(stristr($tipo,"text"))$value.=$campo."='".$data[$campo]."', ";
			if(stristr($tipo,"int"))$value.=$campo."=".$data[$campo].", ";
			if(stristr($tipo,"blob"))$value.=$campo."='".$data[$campo]."', ";
					
		}	
		
		$value=trim($value,", ");	
		$sql="UPDATE ".$tabla." SET ".$value." WHERE ".$condicion;
		//echo $sql;		
		$this->connect();
		if($result=mysql_query($sql)){
			$this->close();
			return true;
		}
		$this->close();
		return false;					
	}
	
	function delete($tabla, $condicion){
		$sql="DELETE ".$tabla." WHERE ".$condicion;
		$this->connect();
		if($result=mysql_query($sql)){
			$this->close();
			return true;
		}
		$this->close();
		return false;
	}
	
	function describe_tabla($tabla){
		$sql="DESCRIBE ".$tabla;
		$this->connect();
		$res=mysql_query($sql);
		while($field=mysql_fetch_assoc($res)){
			$output[]=array('Field'=>$field['Field'], 'Type'=>$field['Type']);
		}
		$this->close();
		return $output;
	}	
	
}
?>
Return current item: vladzur Database