Classe objet dao couche d'accès à mysql data access object

Contenu du snippet

/*
------------DAO------------
----DataAcessObject------
EN fait, c'est une layer au-dessus de MySQL

Ne marche que si ta connexion à Mysql a déjà été activée !!!!
mon conseil : fait 2 fichiers :
- req_connect.php // connecte à la BD MySQL
- req_disconnect.php // mysql_free = libère la connexion

Comment marche la classe DAO ?
Exemple concret :
  • /


include_once("class.dao.php4");

$dao_object = new DAO(); // instancie le nouvel object dao_object de type DAO
$query = "SELECT * FROM table WHERE id=1";
$dao_object->DAO_query($query); // lance la requete et récupère le résultat
$dao_object->DAO_numrows(); // nombre d'enregistrements retournés

// requête UPDATE INSERT
$query = "INSERT INTO table (col1, col2) VALUES (1,'text')";
$dao_object->DAO_execute($query); // unbuffered query = sans retour de ligne
$dao_object->DAO_affrows(); // number of affected rows pour les requêtes UPDATE / INSERT / DELETE / DROP

pour afficher, 3 possibilité comme mysql :
while($val = $dao_object->DAO_fetch_object())
{
echo $val->id;
}

while($val = $dao_object->DAO_fetch_array())
{
echo $val["col1"]; // le nom de la colonne
// ou encore :
echo $val[0]; // le numéro de la colonne en commançant par zéro
}

while($val = $dao_object->DAO_fetch_assoc())
{
echo $val["col2"]; // le nom de la colonne seulement
}

/*
Attention, si tu as un object $this->dao_object dans une de tes classes,
et qui tu t'en sers dans plusieurs méthodes, tu vas avoir un conflit entre les 2 méthodes.
Il faut donc instancier un objet par boucle while, exemple :
  • /


while($val1 = $dao_object1->DAO_fetch_assoc())
{
while($val2 = $dao_object2->DAO_fetch_object())
{
echo $val1["col1"] . " is not equal to " . $val2["col1"];
}
}

Source / Exemple :


<?php
/**

  • class DAO
  • DataAccessObject
  • @version 0.1
  • @copyright 2006
  • /
class DAO{ var $dao_query = NULL; var $dao_result = NULL; var $badquery = NULL; /**
  • Throws an error
  • @param : nothing, uses the last query
  • @return : die and display the error message
  • /
function DAO_error(){ $this->badquery = "Erreur dans la reqête."; die($this->badquery . ":" . $this->dao_query . "<br />" . mysql_error()); } /**
  • Sets the query and sends the query to recover a result
  • @param : all SQL query types (SELECT, INSERT, UPDATE ...)
  • @return : nothing, sets the query and resultset
  • /
function DAO_query($dao_query){ $this->dao_query = $dao_query; $this->dao_result = mysql_query($dao_query) or $this->DAO_error(); } /**
  • Sets the query and sends the query to recover a result
  • For UPDATE, INSERT, DELETE operations
  • @param : a SQL query (UPDATE, INSERT, DELETE)
  • @return : nothing, sets the query and resultset without waiting
  • /
function DAO_execute($dao_query){ $this->dao_query = $dao_query; $this->dao_result = mysql_unbuffered_query($dao_query) or $this->DAO_error(); } /**
  • Retruns the number of rows returns by the last query
  • @param : nothing
  • @return : the number of rows returned in the the last resultset
  • For SELECT queries
  • /
function DAO_numrows(){ $dao_numrows = mysql_num_rows($this->dao_result); return $dao_numrows; } /**
  • Returns the number of affected rows in the last operation
  • @param : nothing
  • @return : the number of rows affected by the last UPDATE, INSERT, DELETE query
  • /
function DAO_affrows(){ $dao_affrows = mysql_affected_rows(); return $dao_affrows; } /**
  • Returns the number of fields returned by the last request
  • @param : nothing
  • @return : the number of fields contened if the last resultset
  • /
function DAO_num_fields(){ $dao_num_fields = mysql_num_fields($this->dao_result); return $dao_num_fields; } /**
  • Returns the field name of the field
  • @param : indice of the field in the query
  • @return : the name of the field at the $indice position
  • /
function DAO_field_name($indice){ $dao_field_name = mysql_field_name($this->dao_result,$indice); return $dao_field_name; } /**
  • Returns an object containing the fields of the query
  • @param : nothing takes the last resultset
  • @return : an object accessible with the query fields
  • /
function DAO_fetch_object(){ $dao_object = mysql_fetch_object($this->dao_result); //or die($bad_query); return $dao_object; } /**
  • Return an associative array only
  • @param : nothing, takes the last resutset
  • @return : an associative array
  • /
function DAO_fetch_assoc(){ $dao_assoc = mysql_fetch_assoc($this->dao_result); return $dao_assoc; } /**
  • Returns a mixed array
  • @param : nothing, takes the last resutset
  • @return : a mixed array
  • /
function DAO_fetch_array(){ $dao_array = mysql_fetch_array($this->dao_result); return $dao_array; } /**
  • Returns the last auto_increment id
  • @param : the identifier and the table name
  • @return : the last auto_increment id
  • /
function DAO_last_id($id,$table){ $li=mysql_query("SELECT $id FROM $table ORDER BY $id DESC LIMIT 0,1"); $r=mysql_fetch_assoc($li); if (mysql_num_rows($li)==1) { return $r[$id]; }else{ return NULL; } } /**
  • Save the transaction at the name given in parameter
  • @param : the SAVEPOINT name
  • @return : nothing, sets the query and resultset
  • /
function DAO_savepoint($dao_savepoint){ $this->dao_query="SAVEPOINT $dao_savepoint"; $this->dao_result=mysql_unbuffered_query($this->dao_query) or $this->DAO_error(); } /**
  • Starts a transaction
  • @param : nothing, takes the last resutset
  • @return : nothing, sets the resultset
  • /
function DAO_start_transaction(){ $this->dao_query = "START TRANSACTION;"; $this->dao_result=mysql_unbuffered_query($this->dao_query) or $this->DAO_error(); $this->dao_query = "SET AUTOCOMMIT=0;"; $this->dao_result=mysql_unbuffered_query($this->dao_query) or $this->DAO_error(); } /**
  • Perform a COMMIT to the database
  • @param: nothing
  • @return : nothing, sets the query and resultset
  • /
function DAO_commit(){ $this->dao_query = "COMMIT"; $this->dao_result = mysql_unbuffered_query("COMMIT") or $this->DAO_error(); } /**
  • Rollback To Savepoint
  • @param : the SAVEPOINT
  • @return : nothing, sets the query and resultset
  • /
function DAO_r2s($dao_savepoint){ $this->dao_query = "ROLLBACK TO $dao_savepoint"; $this->dao_result = mysql_unbuffered_query("ROLLBACK TO $dao_savepoint") or $this->DAO_error(); } /**
  • Rollback to the last transaction started
  • @param : nothing
  • @return : nothing, sets the query and resultset
  • /
function DAO_rollback(){ $this->dao_query = "ROLLBACK"; $this->dao_result = mysql_unbuffered_query("ROLLBACK") or $this->DAO_error(); } } ?>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.