Classe de connexion MySQL
Posté : 11 sept. 2005, 10:53
Ce bout de code est une réponse au message de delcedo galaxialord. Je vous présente ici une petite classe gérant une connexion MySQL et les fonctions s'articulant autour de celle-ci.
<?php
class SQL
{
private $db_id = false;
private $dbname = '';
private $host = '';
private $pass = '';
private $user = '';
public $res = '';
public $row = array();
public $num = 0;
//
// CONSTRUCTEUR
public function __construct
(
$host = '', // string Adresse du serveur MySQL
$user = '', // string Nom d'utilisateur
$pass = '', // string Mot de passe
$dbname = '', // string Nom de la base de données
$connect = true // boolean Connexion lors de la création de l'objet
)
{
$this->host = (string)$host;
$this->pass = (string)$pass;
$this->user = (string)$user;
$this->dbname = (string)$dbname;
if ( (bool)$connect === true )
{
// Etablie la connexion à la base de données
$this->connect();
}
} // end of the "__construct()" function
//
// FERME LA CONNEXION
public function __destruct
(
// void
)
{
// Ferme la connexion si celle-ci est active
$this->close();
} // end of the "__destruct()" function
//
// FERME LA CONNEXION
public function close
(
// void
)
{
// Ferme la connexion si celle-ci est active
if ( $this->db_id )
{
if ( $this->res )
mysql_free_result( $this->res );
return mysql_close( $this->db_id );
}
else
{
return false;
}
} // end of the "close()" function
//
// CONNEXION A LA BDD
public function connect
(
// void
)
{
// Evite d'ouvrir une deuxième connexion
if ( (bool)$this->db_id === false )
{
// Connexion au serveur MySQL
$this->db_id = mysql_connect
(
$this->host,
$this->user,
$this->pass
);
if ( $this->db_id )
{
// Selection de la base de données
if ( ! mysql_select_db( $this->dbname ) )
{
// Fermeture de la connexion si la connexion à la base
// de données échoue
$this->close();
}
return $this->db_id;
}
else
{
return false;
}
}
else
{
return $this->db_id;
}
} // end of the "connect()" function
//
// RETOURNE UN TABLEAU CONTENANT L'ERREUR SQL ET LE SON NUMERO
public function error
(
// void
)
{
return array
(
'errno' => mysql_errno( $this->db_id ),
'error' => mysql_error( $this->db_id )
);
} // end of the "error()" function
//
// RETOURNE UN TABLEAU CONTENANT LES RESULTATS
public function fetch_array
(
$res = false // resource Ressource de la requête SQL
)
{
if ( (bool)$res === false )
$res = $this->res;
$this->row = mysql_fetch_array( $res );
return $this->row;
} // end of the "fetch_array()" function
//
// DERNIER ID INSERE
public function next_id
(
// void
)
{
$this->res = mysql_insert_id( $this->db_id );
return $this->res;
} // end of the "next_id()" function
//
// COMPTE LE NOMBRE DE RESULTATS
public function num_rows
(
$res = false // resource Ressource de la requête SQL
)
{
if ( (bool)$res === false )
$res = $this->res;
$this->num = mysql_num_rows( $res );
return $this->num;
} // end of the "num_rows()" function
//
// EXECUTE LA REQUETE $query
public function query
(
$query // string Requête SQL à exécuter sur le serveur
)
{
$this->num = 0;
$this->row = array();
$this->res = mysql_query( $query, $this->db_id );
return $this->res;
} // end of the "query()" function
} // en of the "SQL" class
?>
Edit: Ajout de la fonction __destruct()