Classe de gestions de requêtes mysql

cs_choy Messages postés 30 Date d'inscription jeudi 26 juin 2003 Statut Membre Dernière intervention 18 juillet 2019 - 3 juin 2008 à 15:51
cs_choy Messages postés 30 Date d'inscription jeudi 26 juin 2003 Statut Membre Dernière intervention 18 juillet 2019 - 3 juin 2008 à 16:04
Alors voilà, je suis en train de faire une class pour gérer mes requêtes sql. Pour le moment ça fonctionne bien, je devrais virer tout ce qui se rapporte au maintien de la connexion (fonctionnalité que je pensai peut être utile pour d'autre mais qui ne l'est pas pour moi et donc je n'ai pas trouvé de bonne méthode pour l'implémenter... pas beaucoup essayé non plus ^^) alors n'y prettez pas trop attention.
Pour le moment donc, cette classe m'a pleinement satisfait mais là je bloc sur une nouvelle fonctionnalité.
Mais tout d'abord voici la classe :
<?php

class Mysql
{
private $db_host = 'localhost';
private $db_login = 'root';
private $db_password = 'root';
public $base = 'ma_base';

protected $db = false;
protected $stay_open = false;
protected $table = '';
protected $where_string = '';
protected $group_string = '';
protected $order_string = '';

function connect()
{
if(!$this-??>db){
$this->db = mysql_connect($this->db_host, $this->db_login, $this->db_password);
mysql_select_db($this->base, $this->db);
mysql_query("SET NAMES 'utf8'", $this->db);
}
}

function disconnect()
{
mysql_close($this->db);
$this->db = false;
}

function where($where_string,$operator='AND')
{
if(empty($this->where_string)){
$this->where_string = ' WHERE '.$where_string;
}else{
$this->where_string .= ' '.$operator.' '.$where_string;
}
}

function group($field_string)
{
$this->group_string = ' GROUP BY '.$field_string;
}

function order($order_string, $way='ASC')
{
if(empty($this->order_string)){
$this->order_string = ' ORDER BY '.$order_string.' '.$way;
}else{
$this->order_string .= ', '.$order_string.' '.$way;
}
}

function unprotect_data($data)
{
if(is_array($data)){
foreach($data as $k=>$i){
$output[$k]=$this->unprotect_data($i);
}
} else {
$output = htmlspecialchars(stripslashes($data));
}

return $output;
}

function protect_data($data)
{
if(is_array($data)){
foreach($data as $k=>$i){
$output[$k]=$this->protect_data($i);
}
} else {
if(get_magic_quotes_gpc()){
if(ini_get('magic_quotes_sybase')) {
$data = str_replace("''", "'", $data);
} else {
$data = stripslashes($data);
}
}
$output = mysql_real_escape_string($data, $this->db);
}

return $output;
}
}

class Select extends Mysql
{
private $select_string = '*';
private $join_string = '';
public $selected_rows;
private $limit_string = '';

function Select($table,$select_string='*',$stay_open=false)
{
$this->table = $table;
$this->select_string = $select_string;
$this->stay_open = $stay_open;

}

function join_table($type,$table,$link)
{
$this->join_string .= ' '.$type.' JOIN '.$table.' ON '.$link;
}

function limit($limit=''){
$this->limit_string = $limit;
}

function query()
{
$this->connect();
$this->query_string = 'SELECT '.$this->select_string.' FROM '.$this->table.$this->join_string.$this->where_string.$this->group_string.$this->order_string.$this->limit_string;
$this->selected_rows = mysql_query($this->query_string, $this->db)or die($this->query_string.'

'.mysql_error($this->db));
echo $this->query_string.'

';
$this->counted_rows = mysql_num_rows($this->selected_rows);
$this->num_rows = mysql_num_rows($this->selected_rows);
if(!$this->stay_open){
$this->disconnect();
}
}
}

class Count extends Mysql
{
private $count_string = '*';
private $join_string = '';
public $selected_rows;
public $counted_rows;

function Count($table,$count_string='*',$stay_open=false)
{
$this->table = $table;
$this->count_string = $count_string;
$this->stay_open = $stay_open;
$this->counted_rows = false;
}

function join_table($type,$table,$link)
{
$this->join_string .= ' '.$type.' JOIN '.$table.' ON '.$link;
}

function query()
{
$this->connect();
$this->query_string = 'SELECT COUNT('.$this->count_string.') FROM '.$this->table.$this->join_string.$this->where_string;
$this->selected_rows = mysql_fetch_array(mysql_query($this->query_string, $this->db));
$this->counted_rows = $this->selected_rows[0];
// echo $this->query_string.'

';
if(!$this->stay_open){
$this->disconnect();
}
}
function num_rows()
{
if(!$this->counted_rows){
$this->query();
}
return $this->counted_rows;

}
}

class Insert extends Mysql
{
private $field_string = '';
private $value_string = '';
private $join_string = '';
public $selected_rows;
public $insert_id;

function Insert($table,$values,$stay_open=false)
{
$this->connect();
$this->table = $table;
$this->stay_open = $stay_open;
$this->add_values($this->protect_data($values));
}

function add_values($values){
foreach($values as $field => $value){
if($value == 'CURRENT_TIMESTAMP'){
if(empty($this->field_string)){
$this->field_string = $field;
$this->value_string = $value;
}else{
$this->field_string .= ','.$field;
$this->value_string .= ','.$value;
}
}else{
if(empty($this->field_string)){
$this->field_string = $field;
$this->value_string = '\''.$value.'\'';
}else{
$this->field_string .= ','.$field;
$this->value_string .= ',\''.$value.'\'';
}
}
}
}

function query()
{
$this->connect();
$this->query_string = 'INSERT INTO '.$this->table.'('.$this->field_string.') VALUES('.$this->value_string.')';
mysql_query($this->query_string, $this->db) or die(mysql_error());
$this->insert_id = mysql_insert_id($this->db);
// echo $this->query_string.'

';
if(!$this->stay_open){
$this->disconnect();
}
}
}

class Update extends Mysql
{
private $value_string = '';
public $selected_rows;

function Update($table,$values,$stay_open=false)
{
$this->table = $table;
$this->stay_open = $stay_open;
$this->add_values($values);
}

function add_values($values){
foreach($values as $field => $value){
if($value == 'CURRENT_TIMESTAMP'){
if(empty($this->value_string)){
$this->value_string = ' '.$field.'='.$value;
}else{
$this->value_string .= ','.$field.'='.$value;
}
}else{
if(empty($this->value_string)){
$this->value_string = ' '.$field.'=\''.$value.'\'';
}else{
$this->value_string .= ','.$field.'=\''.$value.'\'';
}
}
}
}

function query()
{
$this->connect();
$this->query_string = 'UPDATE '.$this->table.' SET'.$this->value_string.$this->where_string;
$this->req = mysql_query($this->query_string, $this->db)or die(mysql_error());
// echo $this->query_string.'

';
if(!$this->stay_open){
$this->disconnect();
}
}
}

class Delete extends Mysql
{

function Delete($table,$where_string='')
{
$this->table = $table;
if(!empty($where_string)){
$this->where($where_string);
}
}

function query()
{
$this->connect();
$this->query_string = 'DELETE FROM '.$this->table.$this->where_string;
mysql_query($this->query_string, $this->db)or die(mysql_error());
// echo $this->query_string.'

';
if(!$this->stay_open){
$this->disconnect();
}
}
}

function unprotect_data($data)
{
if(is_array($data)){
foreach($data as $k=>$i){
$output[$k]=unprotect_data($i);
}
} else {
$output = htmlspecialchars(stripslashes($data));
}

return $output;
}

?>

Je ne sais pas si elle peut être utile mais j'y trouve pas mal d'avantage. Exemple factice de select avec liaison :

$sql = new Select('film');
$sql->join_table('left','realisateurs','film_realisateur = realisateur_id');
$sql->where('film_id = 1');
$sql->where('film_id = 2','or');
$sql->query();

while($row = mysql_fetch_assoc($sql->selected_rows)){
$row = unprotect_data($row);
echo $row['film_titre'].' - '.$row['realisateur_nom'];
}

Maintenant j'aimerai améliorer la function where(), pour le moment elle me permet de mettre des :
WHERE field1 'string1' AND field2 'string2' OR field3 != 'string3'...
et j'aimerai lui faire faire ça :
WHERE (field1 'string1' OR field2 'string2') AND (field3 = 'string3' OR field4 = 'string4')...

Je ne vois pas comment implémenter ça tout en gardant une syntaxe simple.

Si quelqu'un a une idée je suis preneur !
Sinon, si certains trouvent cette classe utile je la posterai dans les sources. À noter que la $sql->limit() prend en param 'LIMIT 0,5'. Si vous comptez utiliser cette classe il est préférable de changer ça pour qu'elle prenne en param '0,5'... me demandai pas pourquoi c'est pas fait ^^.

1 réponse

cs_choy Messages postés 30 Date d'inscription jeudi 26 juin 2003 Statut Membre Dernière intervention 18 juillet 2019
3 juin 2008 à 16:04
woops, le formatage crain ^^ je vous copie tout là :
http://cahnory.free.fr/class_mysql.rtf
0
Rejoignez-nous