Source: lesson_14_e0_writing_in_mysql.php | Résultat |
<?php //Début du programme require ("../mysql_connexion/mysql_connexion.php"); require ("../commons/functions.php"); $tableName = "atelierphp_exe_pass_words" ; //$table_name = "atelierphp_exe_pass_words" ; //on enlève la table si elle existait déjà //bien sûr il faudrait une clé primaire sur l'utilisateur if (mySQLtable_exist($tableName)) { $result = mysqli_query($mysqli,"select * from atelierphp_exe_pass_words "); $nb_rows = mysqli_num_rows($result); if ($nb_rows >19 ) { echo "Arrivé à 20 enregistrements, j'efface la table et je la recrée"; drop_table($tableName); $result = mysqli_query($mysqli,"CREATE TABLE $tableName (PASS_WORD_USER CHAR(9) not null , PASS_WORD_PASS TEXT )"); if ($result == 1) { echo "table created<br/>"; } else { echo "error creating the table<br/>"; } } } $user_name = "Maria" ; $user_pass = "maria_pass" ; insert_a_row($user_name,$user_pass); $user_name = "Laura" ; $user_pass = "laura_pass" ; insert_a_row($user_name,$user_pass); //read the table echo "<table border=1><tr><th>User</th><th>Pass</th></tr>"; $result = mysqli_query($mysqli,"select * from atelierphp_exe_pass_words "); $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>"; //---------------------------------------------------------------- function insert_a_row($user_name,$user_pass) { global $mysqli; require_once("../mysql_connexion/mysql_connexion.php"); $result = mysqli_query($mysqli,"INSERT INTO atelierphp_exe_pass_words (PASS_WORD_USER, PASS_WORD_PASS) VALUES ('$user_name','$user_pass')"); if ($result == 1) { echo "value inserted<br/>"; } else { echo "error inserting in the table the table<br/>"; } } ?>
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à error inserting in the table the table error inserting in the table the table
User | Pass |
---|
Laura | laura_pass | Maria | maria_pass |
|