<?php
/********************************************************************/
// class Db(V0.01) : Db.php
/********************************************************************/
class Db{
var $db;
var $user;
var $password;
var $server;
var $server_type;
var $connection;
var $result;
var $data;
var $query_titles;
var $query_type;
/********************************************************************/
/* includes config file (config.php) whith the connection params of the data base
* includes the file of the specified data base server (default mysql)
* connects to the database
*/
function Db($user_type, $server = 0){
include_once("config.php");
$this->server = $DB[$server]["server"];
$this->db = $DB[$server]["data_base"];
$this->user = $USER[$user_type]["nombre"];
$this->password = $USER[$user_type]["password"];
$this->result;
$this->data ;
$this->server_type = $DB[$server]["server_type"];
$this->query_type = false;
switch($this->server_type){
case "mysql" :{
include_once("data_bases/mysql.php");
break;
}
case "mssql" :{
include_once("data_bases/mssql.php");
break;
}
case "interbase" :{
include_once("data_bases/interbase.php");
break;
}
default :{
include_once("data_bases/mysql.php");
}
}
$this->connection = connect($this->server,$this->user,$this->password);
select_db($this->db, $this->connection);
}
/********************************************************************/
/* returns a link to the query's result
*/
function result(){
return $this->result;
}
/********************************************************************/
/* returns an array whith the pairs :
* [number of the row in the result] => value of the field $name
*/
function field($name){
if(is_int($name)) $name = $this->query_titles[$name];
foreach($this->data as $fila){
$f_array[] = $fila[$name];
}
if(is_array($f_array)) return $f_array;
else return false;
}
/********************************************************************/
/* selects the rows where $field(name of the field in the query) = $value
* returns an array of two dimensions whith the pairs :
* [number of the row ] [name of the field] => value of the field's value that is equal to $value
*/
function rows($value, $field){
foreach($this->data as $row){
if($row[$field] == $value)$aux[] = $row;
}
if(is_array($aux)) return $aux;
else return false;
}
/********************************************************************/
/* selects the first row where $field = $value
* ($field is the name of the field in the query or a numeric external index)
* returns an array whith the pairs :
* [number of the row in the result] => value of the field $name
* (to use with unique keys or to extrac rows with a external index)
*/
function row($value, $field = false){
if($field){
foreach($this->data as $row){
if($row[$field] == $value){
$aux = $row;
break;
}
}
}
else $aux = $this->data[$value];
if(is_array($aux)) return $aux;
else return false;
}
/********************************************************************/
/* returns an array array of two dimensions whith the pairs :
* [number of the row ] [name of the field] => value of the field's value that is equal to $value
*/
function data(){
switch($this->query_type){
case "select" :{
if(is_array($this->data))return $this->data;
break;
}
case "insert" :{
return false;
break;
}
case "update" :{
return false;
break;
}
case "delete" :{
return false;
break;
}
default :{
}
}
}
/********************************************************************/
/* show the data of the query in a html table
*/
function show_data(){
if($this->query_type){
switch($this->query_type){
case "select" :{
echo "\n<table border='1'>\n\t<tr>\n";
foreach($this->query_titles as $clave){
echo "\t\t<td><b>".$clave."</b></td>\n";
}
echo "\t</tr>\n";
foreach($this->data as $fila){
echo "\t<tr>\n";
if($fila){
foreach($fila as $clave => $columna){
echo "\t\t<td>".$columna."</td>\n";
}
}else echo "\t\t<td>No data</td>\n";
echo "\t</tr>\n";
}
echo "</table>\n";
break;
}
case "insert" :{
echo "\n<table border='1'>\n\t<tr>\n";
echo "\t\t<td><b>".$this->data."</b></td>\n";
echo "</table>\n";
break;
}
case "update" :{
echo "se ejecuta con sentencias update";
break;
}
case "delete" :{
echo "se ejecuta con sentencias delete";
break;
}
default :{
echo "se ejecuta por defecto";
}
}
}
else{
echo "se ejecuta si no se ha hecho la query";
}
}
/********************************************************************/
/* executes the query $query
* loads the array $data with the data given back by the query
* loads the array $query_titles with the names of the fields
*/
function query($query){
$this->clear_data();
$this->query_type = strtolower(strtok ($query," "));
$this->result = query($query, $this->connection);
switch($this->query_type){
case "select" :{
$this->data = load_data($this->result);
if(is_array($this->data)) $this->query_titles = array_keys($this->data[0]);
else $this->query_titles[0] = false;
break;
}
case "insert" :{
$this->data = insert_id($this->connection);
break;
}
case "update" :{
break;
}
case "delete" :{
break;
}
default :{
}
}
}
/********************************************************************/
/* closes the data base connection
* free the result of the query
*/
function close(){
switch($this->query_type){
case "select" :{
if($this->result) free_result($this->result);
break;
}
case "insert" :{
if($this->result) unset($this->result);
break;
}
default :{
}
}
if($this->connection) close($this->connection);
}
/********************************************************************/
/* cleans the data kept in the object
*/
function clear_data(){
unset($this->data);
unset($this->query_titles);
unset($this->query_type);
}
/********************************************************************/
}
/********************************************************************/
// friends functions to work with two dimensions arrays
/********************************************************************/
/* verifies if $aguja is in $array
*/
function is_in_array($aguja, &$array){
$encontrado = false;
if(is_array($array)){
foreach($array as $fila){
if(is_array($fila)){
foreach($fila as $clave => $columna){
if($columna == $aguja){
$encontrado = true;
break;
}
if ($encontrado) break;
}
}
}
}
return $encontrado;
}
/********************************************************************/
/* Returns the maximum value of $array
*/
function max_value(&$array){
$max = 0;
if(is_array($array)){
foreach($array as $fila){
if(is_array($fila)){
foreach($fila as $clave => $columna){
if($columna > $max){
$max = $columna;
}
}
}
}
}
return $max;
}
/********************************************************************/
?>