Premiers "select" dans la BDD.

This commit is contained in:
Georges Dupéron 2010-10-11 00:03:14 +02:00
parent e4a330f5b5
commit 32c1b57387
3 changed files with 38 additions and 21 deletions

View File

@ -22,7 +22,7 @@
class BDD { class BDD {
private static $handle = null; private static $handle = null;
public static function get() { public static function get() {
if (self::$handle === null) { if (!is_resource(self::$handle)) {
self::$handle = @mysql_connect( self::$handle = @mysql_connect(
Config::get('db_hôte'), Config::get('db_hôte'),
Config::get('db_utilisateur'), Config::get('db_utilisateur'),
@ -31,7 +31,6 @@ class BDD {
if (!is_resource(self::$handle)) { if (!is_resource(self::$handle)) {
Debug::error("Échec à la connexion à la base de données"); Debug::error("Échec à la connexion à la base de données");
} }
mysql_select_db(Config::get('db_base'), self::$handle) or Debug::sqlerror();
self::init(); self::init();
} }
return self::$handle; return self::$handle;
@ -46,6 +45,8 @@ class BDD {
} }
public static function init() { public static function init() {
self::unbuf_query("create database if not exists " . Config::get('db_base'));
mysql_select_db(Config::get('db_base'), self::$handle) or Debug::sqlerror();
self::unbuf_query('create table if not exists ' . self::table("pages") . ' (' self::unbuf_query('create table if not exists ' . self::table("pages") . ' ('
. 'uid_page integer auto_increment primary key' . 'uid_page integer auto_increment primary key'
. ')'); . ')');
@ -55,12 +56,23 @@ class BDD {
. 'groupe char(10)' . 'groupe char(10)'
. ')'); . ')');
self::unbuf_query('create table if not exists ' . self::table("proprietes") . ' (' self::unbuf_query('create table if not exists ' . self::table("proprietes") . ' ('
. 'uid_prop integer,' . 'uid_prop integer auto_increment primary key,'
. 'uid_page integer,' . 'uid_page integer,'
. 'systeme bool,' . 'systeme bool,'
. 'nom char(30),' . 'nom char(30),'
. 'valeur char' . 'valeur char'
.')'); .')');
self::test();
}
public static function test() {
// Insertion de la racine :
self::modify("insert into " . self::table("pages") . " values(0)");
self::modify("insert into " . self::table("proprietes") . " values(0, 0, true, 'date_creation', 0)");
self::modify("insert into " . self::table("proprietes") . " values(0, 0, true, 'date_modification', 0)");
self::modify("insert into " . self::table("proprietes") . " values(0, 0, true, 'publier', 'true')");
self::modify("insert into " . self::table("proprietes") . " values(0, 0, true, 'nom_systeme', 'racine')");
self::modify("insert into " . self::table("proprietes") . " values(0, 0, true, 'composant_url', '')");
} }
public static function unbuf_query($q) { public static function unbuf_query($q) {
@ -68,23 +80,29 @@ class BDD {
mysql_unbuffered_query($q . ";", self::get()) or Debug::sqlerror(); mysql_unbuffered_query($q . ";", self::get()) or Debug::sqlerror();
} }
public static function select($cols, $table, $filter = "") {
$q = "select $cols from " . self::table($table) . " $filter;";
debug::info("sql : " . $q);
$qres = mysql_query($q, self::get()) or Debug::sqlerror();
$ret = array();
while ($row = mysql_fetch_array($qres)) {
$ret[] = $row;
}
return $ret;
}
public static function modify($q) {
debug::info("sql : $q;");
mysql_query($q . ";", self::get()) or Debug::sqlerror();
}
public static function table($nom) { public static function table($nom) {
return Config::get('db_prefixe') . $nom; return Config::get('db_prefixe') . $nom;
} }
public static function test() {
$result = mysql_query("SELECT id, name FROM mytable") or Debug::sqlerror();
echo "<br/><br/>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID : %s Nom : %s", $row[0], $row[1]);
}
mysql_free_result($result);
}
public static function close() { public static function close() {
mysql_close(self::get()); mysql_close(self::get()) or Debug::sqlerror();
self::$handle = null;
} }
} }

View File

@ -3,9 +3,6 @@
function main() { function main() {
$bdd = BDD::get(); $bdd = BDD::get();
BDD::reset(); BDD::reset();
BDD::close();
Debug::afficher();
exit;
$g = new AdminListeUtilisateurs(); $g = new AdminListeUtilisateurs();
@ -14,6 +11,7 @@ function main() {
echo htmlspecialchars($p->to_XHTML_5()); echo htmlspecialchars($p->to_XHTML_5());
echo "</pre>"; echo "</pre>";
BDD::close();
Debug::afficher(); Debug::afficher();
} }

View File

@ -60,10 +60,10 @@ class Page {
niy("composant_url"); niy("composant_url");
} }
private $uid = -1; private $uid = 0;
public function uid() { public function uid() {
// Renvoie l'uid de la page dans la base de données. // Renvoie l'uid de la page dans la base de données.
niy("uid"); return $this->uid;
} }
/* public function select($requête) { /* public function select($requête) {
@ -78,7 +78,8 @@ class Page {
// ordre = null => ordre = "date_creation desc" // ordre = null => ordre = "date_creation desc"
// limit = null || limit = 0 => pas de limite // limit = null || limit = 0 => pas de limite
// offset = null => offset = 0 // offset = null => offset = 0
niy("enfants"); var_dump(BDD::select("uid_page", "enfants", "where uid_page_parent = " . $this->uid()));
niy("enfants__");
} }
public function ajouter_enfant($type, $groupe = "main") { public function ajouter_enfant($type, $groupe = "main") {