game/onyx2/modules/db/mysql.class.php

164 lines
4.6 KiB
PHP
Raw Normal View History

2009-11-01 11:00:00 +00:00
<?php
class BDD
{
var $connected;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
private $session;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
private $reponse;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
var $host;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
var $user;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
private $password;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
var $database;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
var $num_rows;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
var $nodb;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function __construct($profile=NULL)
{
if($profile === FALSE) return FALSE;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
global $db_config;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if(empty($profile))
{
if(!$db_config['profile']) return FALSE;
$profile = &$db_config['profile'];
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if(!ctype_alnum($profile)) trigger_error('Le nom du profil contient des caracteres illegaux',E_USER_ERROR);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if($db_config['profile'])
{
require(ONYX.'db/'.$profile.'.profile.php');
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
$db = &$___profile['db'];
$host = &$___profile['host'];
$user = &$___profile['user'];
$pass = &$___profile['pass'];
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if($db_config['crypt']) $pass = dbpass($pass,$db_config['crypt']);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
return $this->connexion($host,$user,$pass,$db);
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function connexion($host,$user,$pass,$db=NULL)
{
if($this->session) $this->deconnexion();
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
$this->reponse = NULL;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$this->session = mysqli_connect($host,$user,$pass);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if(!$this->session)
{
elog('Connexion impossible a la base de donnee : '.$this->erreur(),2);
if(function_exists($this->nodb)) call_user_func($this->nodb);
return FALSE;
}
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
mysqli_query($this->session,'SET CHARACTER SET "utf8"');
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
$this->host = $host;
$this->user = $user;
$this->password = $pass;
if($db) $this->db($db);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
$this->connected = TRUE;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function reconnexion()
{
2020-11-15 16:22:13 +00:00
if(!empty($this->host) && !empty($this->user) && !empty($this->password) && !empty($this->database)) $this->connexion($this->host,$this->user,$this->password,$this->database);
2009-11-01 11:00:00 +00:00
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function deconnexion()
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$r = mysqli_close($this->session);
2009-11-01 11:00:00 +00:00
$this->session = FALSE;
$this->connected = FALSE;
return $r;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function erreur()
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
return mysqli_error($this->session);
2009-11-01 11:00:00 +00:00
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function db($db=NULL)
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
return $this->database = mysqli_select_db($this->session,$db) ? $db : $this->database;
2009-11-01 11:00:00 +00:00
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function escape(&$var)
{
if(!$this->session) return FALSE;
2020-11-15 16:22:13 +00:00
$var = mysqli_real_escape_string($this->session,$var);
2009-11-01 11:00:00 +00:00
return $var;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function query($query)
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$this->reponse = mysqli_query($this->session,$query);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
global $db_config;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if(!$this->reponse && $db_config['log']) elog('Erreur Mysql: " '.$this->erreur().' ", avec la requète: { '.$query.' }.',1);
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$this->num_rows = @mysqli_num_rows($this->reponse);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if($this->num_rows == 0) return NULL;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
elseif($this->num_rows >= 1)
{
2020-11-15 16:22:13 +00:00
for($i=0; $var = mysqli_fetch_assoc($this->reponse); $i++) $sortie[$i] = $var;
2009-11-01 11:00:00 +00:00
return $sortie;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
else return FALSE;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function unique_query($query)
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$this->reponse = mysqli_query($this->session,$query);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
global $db_config;
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if(!$this->reponse && $db_config['log']) elog('Erreur Mysql: " '.$this->erreur().' ", avec la requète: { '.$query.' }.',1);
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
$this->num_rows = @mysqli_num_rows($this->reponse);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
if($this->num_rows == 0) return NULL;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
elseif($this->num_rows >= 1) return mysqli_fetch_assoc($this->reponse);
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
else return FALSE;
}
2020-11-15 15:12:32 +00:00
2009-11-01 11:00:00 +00:00
function affected()
{
if(!$this->session) return FALSE;
2020-11-15 15:12:32 +00:00
2020-11-15 16:22:13 +00:00
return mysqli_affected_rows($this->session);
2009-11-01 11:00:00 +00:00
}
}
2020-11-15 16:22:13 +00:00
?>