Ciao vorrei postarti una mia semplice classe per interfacciarsi con il database mysql:
codice:
<?php
/******************************************************************************/
/** CMS SVILUPPATO DA SANTINO BIVACQUA info@miserve.org **/
# ____ _____ __ ___ ______ #
# \ \ / / ____ | \ / | / | #
# \ \/ / / __ \ | \ / || ___| ____ #
# \ / | | |_|| \/ || /___ |####| #
# \ / | | | | /| |\ \ |#|__ #
# / \ | | _ | |\ / | | \___ ||####| #
# / \ | \__| || | \/ | | ___/ | __|#| #
# / /\ \ \____/ | | | || ||####| #
# /____/ \____\ |__| |__||_______/ #
# #
/** XcMSs v. 1.0.0 **/
/******************************************************************************/
/* New CMS for MiServe.org */
/*----------------------------------------------------------------------------*/
/* Classe per connettersi a Mysql */
/*----------------------------------------------------------------------------*/
class DatabaseSql {
var $host,$user,$pw;
var $work,$query_ok;
var $connect_id,$result_id;
var $numero_query = 0;
var $qq;
var $db_selected;
//Costruttore della classe
function DatabaseSql(&$_CONFIG_DB) {
$this->host = &$_CONFIG_DB['host'];
$this->user = &$_CONFIG_DB['user'];
$this->pw = &$_CONFIG_DB['password'];
$this->work = FALSE;
}
//Metodo per connettersi al database
function connect() {
if ($this->connect_id = @mysql_connect($this->host,$this->user,$this->pw)) {
//Imposta work a true in modo che la classe sia abilitata
$this->work = TRUE;
} else {
//Imposta work a false in modo che la classe sia disabilitata
$this->work = FALSE;
die("Connessione al database non riuscita");
}
}
//Metodo per selezionare database
function select($database) {
//Controllo se la classe può lavorare
if ($this->work==FALSE) {return FALSE;}
//Se il database risulta essere già selezionato esco ritornando TRUE
if ($this->db_selected==$database) {return TRUE;}
$this->db_selected = $database;
if (!mysql_select_db($database,$this->connect_id)) {
//Imposta work a false in modo che la classe sia disabilitata
$this->work = FALSE;
die("Selezione del database non riuscita");
}
}
//Metodo per Inviare query al database
function query($query,$unbuffered=TRUE) {
//Controllo se la classe può lavorare
$function = $unbuffered ? 'mysql_unbuffered_query' : 'mysql_query';
if ($this->work==FALSE) {return FALSE;}
if ($this->result_id = @$function($query,$this->connect_id)) {
$this->numero_query++;
$this->qq.=$this->numero_query." => ".$query."<hr />";
$this->query_ok = TRUE;
return TRUE;
} else {
$this->query_ok = FALSE;
echo "Error : <span style=\"color:red\">$query</span>, non riuscita
\n";
return FALSE;
}
}
//Metodo usato per ottenere solo il primo risultato
function first_result_query($query)
{
if ($this->work==FALSE) {return FALSE;}
$this->query($query);
$risultato = $this->fetch();
if ($this->work==FALSE or $this->query_ok==FALSE) {return FALSE;}
$numero_argomenti = func_num_args()-1;
if ($numero_argomenti==0)
{
return $risultato;
}
elseif ($numero_argomenti==1)
{
$var = func_get_arg(1); //Metto 1 e non 2 perche' parte da zero
return $risultato->$var;
}
else
{
$args = func_get_args();
$return = array();
$i = 0;
foreach ($args as $value)
{
if ($i>0) $return[$value] = $risultato->$value;
$i = 1;
}
return $return;
}
}
//Metodo per recuperare risultati
function fetch($type_result_return="o") {
//Controllo se la classe può lavorare
if ($this->work==FALSE or $this->query_ok==FALSE) {return FALSE;}
switch ($type_result_return)
{
case 'a':
$function = 'mysql_fetch_array';
break;
case 'n':
$function = 'mysql_fetch_row';
break;
case 'o':
default:
$function = 'mysql_fetch_object';
}
return $function($this->result_id);
}
//Metodo che ottiene il numero di righe coinvolte nelle precedenti
//operazioni MySQL (insert, update, delete)
function affected_row() {
if ($this->work==FALSE or $this->query_ok==FALSE) {return FALSE;}
return mysql_affected_rows($this->connect_id);
}
//Metodo che restituisce i numeri dei campi estratti de SELECT
function num_rows() {
if ($this->work==FALSE or $this->query_ok==FALSE) {return FALSE;}
return mysql_num_rows($this->result_id);
}
//Metodo per l'escape dei caratteri onde evitare defacciamenti
function escape($string)
{
return mysql_escape_string($string);
}
//Metodo per chiudere connessione al database
function close() {
@mysql_close($this->connect_id);
}
}
?>
Puo' essere migliorata, ma a me a volte va bene cosi' com'e'