Il s'agit d'une petite class (inspirée des class du cakePHP) qui permet de gérer de simples requêtes sous MySQL!
Elle supporte les table avec préfixe.
Source / Exemple :
<?php
////////////////////////////////////////////////////////////////////
// MYSQL
class MySQL
{
# Script réalisé par floptwo (floptwo@gmail.com)
# Plus d'infos sur www.floptwo.com
var $bdd_hote = 'localhost';
var $bdd_user = 'root';
var $bdd_pass = '';
var $bdd_bdd = 'mabdd';
var $bdd_prefix = 'flop_';
var $bdd_connected = false;
var $lastInsertId;
function connection()
{
if (mysql_connect($this->bdd_hote, $this->bdd_user, $this->bdd_pass))
{
if (mysql_select_db($this->bdd_bdd))
{
$this->bdd_connected = true;
}
}
return $this->bdd_connected;
}
function bdd_prefix($table)
{
return $this->bdd_prefix.$table;
}
function query($sql)
{
if($this->connection())
{
$array = array();
$query = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error);
if (eregi("^SELECT",$sql))
{
$mysql_num_rows = mysql_num_rows($query);
if ($mysql_num_rows == 0)
{
$array = false;
}
elseif ($mysql_num_rows > 0)
{
while($row = mysql_fetch_assoc($query))
{
$array[] = $row;
}
}
mysql_free_result($query);
}
if (eregi("^INSERT",$sql))
{
$this->lastInsertId = mysql_insert_id();
}
mysql_close();
if (eregi("^SELECT",$sql))
{
return $array;
}
return true;
}
return false;
}
function findAll($table, $conditions_array = null)
{
if ($conditions_array != null)
{
$i = 0;
$count = count($conditions_array);
$conditions = 'WHERE ';
foreach ($conditions_array as $key => $value)
{
$i++;
if ($key == 'id')
{
$conditions .= "$key = '$value'";
}
else
{
$conditions .= "$key = '$value'";
}
if ($i != $count) $conditions .= ' AND ';
}
}
return $this->query('SELECT * FROM `'.$this->bdd_prefix($table).'` '.$conditions.' ORDER BY id ASC;');
}
function find($table, $conditions_array = null)
{
if ($conditions_array != null)
{
$i = 0;
$count = count($conditions_array);
$conditions = 'WHERE ';
foreach ($conditions_array as $key => $value)
{
$i++;
if ($key == 'id')
{
$conditions .= "$key = '$value'";
}
else
{
$conditions .= "$key = '$value'";
}
if ($i != $count) $conditions .= ' AND ';
}
}
return $this->query('SELECT * FROM `'.$this->bdd_prefix($table).'` '.$conditions.' LIMIT 1;');
}
function findById($table, $id)
{
return $this->find($table, array('id'=>$id));
}
function insertInto($table, $fields = array())
{
$i = 0;
$count = count($fields);
$keys = '(';
$values = '(';
foreach ($fields as $key => $value)
{
$i++;
$keys .= "`$key`";
switch (strtoupper($value))
{
case 'NOW()' : case 'NOW' : $values .= 'NOW()'; break;
default : $values .= "'$value'"; break;
}
if ($i != $count)
{
$keys .= ',';
$values .= ',';
}
}
$keys .= ')';
$values .= ')';
return $this->query('INSERT INTO `'.$this->bdd_prefix($table).'` '.$keys.' VALUES '.$values.';');
}
function getLastId()
{
return $this->lastInsertId;
}
function update($table, $id, $conditions_array)
{
$i = 0;
$count = count($conditions_array);
$conditions = 'SET ';
foreach ($conditions_array as $key => $value)
{
$i++;
$conditions .= "$key = '$value'";
if ($i != $count) $conditions .= ', ';
}
return $this->query('UPDATE `'.$this->bdd_prefix($table).'` '.$conditions.' WHERE id = '.$id.' LIMIT 1;');
}
function del($table, $id)
{
return $this->query('DELETE FROM `'.$this->bdd_prefix($table).'` '.$conditions.' WHERE id = '.$id.' LIMIT 1;');
}
# Script réalisé par floptwo (floptwo@gmail.com)
# Plus d'infos sur www.floptwo.com
}
?>
<?php
// EXEMPLES D'UTILISATION
$sql = new MySQL();
// SELECT
// findAll
$categories = $sql->findAll('categories');
foreach ($categories as $categorie)
{
echo $categorie['id'];
echo $categorie['title'];
}
// find
$categorie = $sql->findById('categories', array('title'=>'flop'));
echo $categorie[0]['id'];
echo $categorie[0]['title'];
// findById
$categorie = $sql->findById('categories', '2');
echo $categorie[0]['id'];
echo $categorie[0]['title'];
// INSERT TO
if ($sql->insertInto('categories', array('title'=>'nouvelle catégorie', 'description'=>'Ma description')))
{
// Voila
}
// après un INSERT TO, voila de quoi récupérer la dernière ligne ajoutée
$newCatId = $sql->getLastId();
// UPDATE
if ($sql->update('categories', '2', array('title'=>'floptwo')))
{
// Voila
}
// DELETE
if ($sql->del('categories', '2'))
{
// Voila
}
?>
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.