Source: lesson_08_db_lecture.php | Résultat |
<?php require ("../mysql_connexion/mysql_connexion.php"); require ("lesson_08_db_connect.php"); $db_sel = new db_select();//instantiation de la classe $db_sel->function_db_connect(); // appel de methode de classe //creation de la table $result = mysqli_query($mysqli,"CREATE TABLE atelierphp_exe_test_8 ( chapter_id DATETIME default CURRENT_TIMESTAMP, chapter_title text, chapter_directory text, chapter_title_2 text )") ; // lecture de la base de données $sql = "SELECT chapter_id, chapter_title FROM atelierphp_exe_test_8"; $db_sel->db_set_select($sql, "2"); $count = $db_sel->db_get_count(); $result = $db_sel->db_get_select(); // affichage des données lue for($r=0; $r<$count; $r++) { echo "<small>".$result[$r][0]." ". $result[$r][1]."</small><br/>"; } If ($count == 0) { echo "Il n'y a aucun enregistrement dans la table, utilisez l'exercice suivant pour en écrite un"; } ?>
Fichier : lesson_08_db_connect.inc
<?php class db_connect { function function_db_connect() { require ("../commons/connect_to_db_no_die.php"); $this->host = $host; $this->user = $user; $this->pass = $pass; $this->database = "atelierpl"; } function get_connect() { mysql_connect($this->host, $this->user, $this->pass); mysql_select_db("atelierpl"); } } //------------------------------------------------ class db_select extends db_connect { var $count; var $row; function function_db_select($count=0, $row="") { $this->count = $count; $this->row = $row; } function db_set_select($sql, $num) { $qry = mysql_query($sql); $this->count = mysql_num_rows($qry); $i=0; while($out = mysql_fetch_array($qry)) { for($j=0; $j<$num; $j++ ) { $this->row[$i][$j] = $out[$j]; } $i++ ; } } function db_get_select() { return $this->row; } function db_get_count() { return $this->count; } } //--------------------------------------------------- class db_exec extends db_connect { var $sql = ""; var $err = ""; function db_set_exec($sql) { if(!mysql_query($sql)) { $this->err = mysql_error(); return false; } else { return true; } } function db_get_error(){ return $this->err; } } ?>
| 2024-11-22 07:19:27 vive les vacances
|