Source: lesson_13_a1_create_password_table.php | Résultat |
<?php require ("../mysql_connexion/mysql_connexion.php"); require ("../commons/functions.php"); //mysql_select_db($db) ; $tableName = "atelierphp_exe_pass_words" ; (mySQLtable_exist($tableName)) ? drop_table($tableName) : print (""); $result = mysqli_query($mysqli,"CREATE TABLE $tableName (PASS_WORD_USER CHAR(19) not null , PASS_WORD_PASS CHAR(19), PRIMARY KEY (PASS_WORD_USER) )"); if ($result == 1) { echo "table $tableName created<br/>"; } else { echo "error creating the table $tableName<br/>"; } ?> <p> <a href="../commons/show_exercises.php?exercise= lesson_13_a2_add_in_password_table.php"> Ajout d'utilisateurs à la table des mots de passe</a><br/> <?php // en pratique, hors de cet environnement, on écrira plutôt : //<a href="lesson_13_a2_add_in_password_table.php".... ?>
Fichier : ../commons/connect_to_db.php
<?php @include ("../mysql_connexion/mysql_connexion.php"); @include ("../../mysql_connexion/mysql_connexion.php"); function mySQLdie($error = "") { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); global $strError,$strSQLQuery ; global $strMySQLSaid, $strBack, $sql_query;
echo "<b> Error : $strError </b>"; if(isset($sql_query) && !empty($sql_query)) { echo "$strSQLQuery: <pre>$sql_query</pre><p>"; } if(empty($error)) echo $strMySQLSaid.mysqli_error($mysqli); else echo $strMySQLSaid.$error; echo "<br/><a href=\"javascript:history.go(-1)\"> $strBack</a>"; exit; } ?>
Fichier : ../commons/functions.php
<?php // Pour savoir si une table existe function mySQLtable_exist($tableName) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $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) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); 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) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $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) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $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="") { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); 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_pass_words Table effacée table atelierphp_exe_pass_words created
Ajout d'utilisateurs à la table des mots de passe
|