Source: lesson_09_a3_create_table_loans.php | Résultat |
<?php require ("../mysql_connexion/mysql_connexion.php"); require ("../commons/functions.php"); //mysql_select_db($db) ; /*Table des emprunts +-----------+----------+ | membre_id | video_id | +-----------+----------+ | 2 | 6 | | 4 | 2 | | 1 | 1 | | 1 | 2 | | 1 | 3 | +-----------+----------+*/ $tableName = "atelierphp_exe_emprunts"; (mySQLtable_exist($tableName)) ? drop_table($tableName) : print (""); //----------------------------------------------------- $result = mysqli_query($mysqli,"CREATE TABLE $tableName (emprunt_video_id INT NOT NULL, emprunt_membre_id INT, PRIMARY KEY (emprunt_video_id) , INDEX (emprunt_video_id))") ; ($result == 1)? print("Table $tableName created<br/>"): print("Error creating the table $tableName<br/>"); // emprunt_DATE TIMESTAMP, ?>
Fichier : connect_to_db.php
<?php //ceci est un fichier fictif, //la vrai connection est déjà //faite par ailleur /* $host = ""; // nom du serveur : localhost, sql5, ... $user = ""; // nom d'utilisateur (root si en local) $pass = ""; // mot de passe $db = ""; // nom de la base de données mySQL $dblink = mysql_connect($host,$user,$pass); //mysql_select_db($db); */ ?>
Fichier : functions.php
<?php // Pour savoir si une table existe function mySQLtable_exist($tableName) { $query = "SELECT COUNT(*) FROM $tableName"; $result = mysqli_query($mysqli,$query); $num_rows = @mysqli_num_rows($result); if($num_rows) { echo "<small>La table existait déjà <br/>"; return TRUE; } else { echo "<small>La table n'existait pas <br/>"; return FALSE; } } // Supprime une table avec message approprié //attention $tableName et non $table_name function drop_table($tableName) { echo "Efface table ".$tableName."<br/>"; $result = mysqli_query($mysqli,"DROP TABLE $tableName") ; if ($result == 1) { echo "Table effacée <br/>"; } else { echo "Erreur en effaçant the table<br/>"; } } // Insère un enregistrement dans une table function insert_a_row_1 ($table_name, $field_name_1, $field_name_2, $field_1, $field_2) { $result = mysqli_query($mysqli,"INSERT INTO $table_name ($field_name_1,$field_name_2) VALUES ('$field_1','$field_2')"); if ($result == 1) { echo "valeur $field_2 insérée dans $field_name_2 <br/>"; } else { echo "erreur lors de l'insersion $field_2 dans la table $table_name <br/>"; } } function insert_a_row_2 ($table_name, $field_name_1, $field_name_2, $field_name_3, $field_1, $field_2, $field_3) { $result = mysqli_query($mysqli,"INSERT INTO $table_name ($field_name_1,$field_name_2,$field_name_3) VALUES ('$field_1','$field_2','$field_3')"); if ($result == 1) { echo "value $field_2 inserted in $field_name_2 <br/>"; } else { echo "error inserting $field_2 in the table $table_name <br/>"; } } //lecture d'une table function readTable ($tableName,$field_1,$field_2="",$field_3="", $field_4="",$field_5="") { echo "Lecture de la table $tableName :"; echo "<table border=1><tr><th>$field_1 </th><th>$field_2</th> <th>$field_3</th><th>$field_4</th> <th>$field_5</th></tr>"; $result=mysqli_query($mysqli,"select * from $tableName "); $fields = mysqli_num_fields($result); while ($row=mysqli_fetch_array($result)) { echo "<tr>"; for($i=0;$i<$fields;$i++) { echo "<td>$row[$i]</td>"; } echo "</tr>"; } echo "</table>"; } ?>
|
La table existait déjà Efface table atelierphp_exe_emprunts Table effacée Table atelierphp_exe_emprunts created
|