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 {
private static $handle = null;
public static function get() {
if (self::$handle === null) {
if (!is_resource(self::$handle)) {
self::$handle = @mysql_connect(
Config::get('db_hôte'),
Config::get('db_utilisateur'),
@ -31,7 +31,6 @@ class BDD {
if (!is_resource(self::$handle)) {
Debug::error("Échec à la connexion à la base de données");
}
mysql_select_db(Config::get('db_base'), self::$handle) or Debug::sqlerror();
self::init();
}
return self::$handle;
@ -46,6 +45,8 @@ class BDD {
}
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") . ' ('
. 'uid_page integer auto_increment primary key'
. ')');
@ -55,12 +56,23 @@ class BDD {
. 'groupe char(10)'
. ')');
self::unbuf_query('create table if not exists ' . self::table("proprietes") . ' ('
. 'uid_prop integer,'
. 'uid_prop integer auto_increment primary key,'
. 'uid_page integer,'
. 'systeme bool,'
. 'nom char(30),'
. '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) {
@ -68,23 +80,29 @@ class BDD {
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) {
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() {
mysql_close(self::get());
mysql_close(self::get()) or Debug::sqlerror();
self::$handle = null;
}
}

View File

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

View File

@ -60,10 +60,10 @@ class Page {
niy("composant_url");
}
private $uid = -1;
private $uid = 0;
public function uid() {
// Renvoie l'uid de la page dans la base de données.
niy("uid");
return $this->uid;
}
/* public function select($requête) {
@ -78,7 +78,8 @@ class Page {
// ordre = null => ordre = "date_creation desc"
// limit = null || limit = 0 => pas de limite
// 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") {