PHP

1.      Page php simple.
2.      Les 3 boucles.
3.      Instruction empty.
4.      Page html intégrant du php.
5.      Suppression d’un enregistrement en php.
6.      Création code aléatoire.
7.      Depuis la base test2.
8.      Utilisation PHP + Mysql + PDO.

1. Page php simple.

<?php
	$name = 'Marie';
	echo $name.'<br/>';
	echo 'texte<br/>';
	echo 18;
?>

2. Les 3 boucles.

Ex :boucle while

<?php
	//Initaliser deux variable
	$nom = 'OLIVIER';
	$longeur = strlen($nom);
	//Initialiser un indice
	$indice = 0; 
	//Tant que l'indice est inférieur à la longeur de la chaîne
	while ($indice < $longeur) {
		//Afficher le caractère correspondant à l'indice suivi
		//d'un point.
		echo "<b>$nom[$indice].</b>";
		//Incrémenter l'indice
		$indice++;
	}
	echo '<br/>';
?>

Ex :boucle do

<?php
	//Initaliser deux variable
	$nom = 'OLIVIER';
	$longeur = strlen($nom);
	//Initialiser un indice
	$indice = 0; 
	//Tant que l'indice est inférieur à la longeur de la chaîne
	do {
		//Afficher le caractère correspondant à l'indice suivi
		//d'un point.
		echo "$nom[$indice].";
		//Incrémenter l'indice
		$indice++;
	} while ($indice < $longeur);
	echo '<br/>';
?>

Ex :boucle for

<?php
	//Utilisation de la structure for pour un tableau
	//à indices entier consécutifs
	//initialisation du tableau
	$couleurs = array('bleu','blanc','rouge');
	$nombre = 3;
	//Boucle utilisant un indice $i qui démarre à 0 ($i = 0)
	//qui est incrémenté d'une unité à chaque itération ($i++);
	//la boucle se poursuit tant que l'indice est inférieur au
	//nombre d'éléments présents dans le tableau ($i < $nombre)
	for ($i = 0; $i <$nombre; $i++) {
		echo "$couleurs[$i]<br/>";
	};
?>

3. Instruction empty.

<?php
	//https://dengfamily.fr/marie/teste15.php
	$nom = 'Dupont';
	$est_vide = empty($nom);
	//echo "\$est_vide =".$est_vide.'<br/>';
	if ($est_vide) {
		echo 'C\'est vide.<br/>';
	} else {
		echo 'Ce n\'est pas vide et $nom = ' .'\''. $nom.'\'<br/>';
	}
		var_dump($nom);
?>

4. Page html intégrant du php.

<!doctype html>
<html>
	<head>
		<title>Navigateur</title>
	</head>
	<body>
		Les informations sur le Navigateur sont :
		<?php
			echo $_SERVER['HTTP_USER_AGENT'];
		?>
	</body>
</html>

5. Suppression d’un enregistrement en php.

<?php
	//connection a la base de données mysql
	$connection = @mysqli_connect('localhost','root','','mabase');
	//effacer l'enregistrement
	$id = @$_GET['ID'];
	if (!empty($id)) {
		//echo ('ID = '.$id.'<br/>');
		$sql = 'delete from nationalite where ID = '.$id;
		//echo ('sql = '.$sql.'<br/>');
		$requete = mysqli_query($connection,$sql);
	}
	//execution de la requete
	$requete = mysqli_query($connection,'SELECT * from nationalite ORDER BY ID');
	
	// affichage du resultat de la requete
	while ($ligne = mysqli_fetch_assoc($requete)) {
		echo $ligne['ID'] . ' ' . $ligne['LIB'] . ' - <a href="?ID='.$ligne['ID'].'">supprimer</a> <br/>';
	}
	//ferme la base de données
	@mysqli_close ($connection);
?>

6. Création code aléatoire.

<?php
//echo chr(122);  //48-57
               //65-90
			   //97-122

	function random_string2($length=6){
	    $i=0;
		$string = '';
		while($i<$length){
			$nb=rand(48,122);
			if (($nb>=48 && $nb<=57)||($nb>=65 && $nb<=90)||($nb>=97 && $nb<=122)) {
				$i++;
				$string .= chr($nb);
			}
		}
		return $string;
	}

	function random_string($length=6){
	    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    $string = '';
	    for($i=0; $i<$length; $i++){
	        //$string .= $chars[rand(0, strlen($chars)-1)];
			$string .= $chars[rand(0, strlen($chars)-1)];
	    }
	    return $string;
	}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Code</title>
	<meta charset="utf-8">
</head>
<body>
	<center>
		<h1>Exercice Code</h1>
		<p>
			Ecrire un script PHP qui calcule peut crée des codes aléatoire.
		</p>
		<form method="post" action="code.php">
			<table border = 0>
			<tr>
				<td>Nouveau code</td>
				<td><input type="submit" name="Valider" value="Valider"></td>
			</tr>
		</table>
		</form>

		<?php
		if(isset($_POST['Valider'])){
			printf(random_string2());
		}
		?>
	</center>
</body>
</html>

7. Depuis la base test2.

CREATE TABLE personne (
  id_personne int(11) auto_increment NOT NULL,
  nom varchar(50) DEFAULT NULL,
  prenom varchar(50) DEFAULT NULL,
  age int(3) DEFAULT NULL,
  date_enregistrement datetime DEFAULT NULL,
  ecole varchar(50) DEFAULT NULL,
  nbannee int(11) DEFAULT NULL,
  primary key(id_personne)
);

INSERT INTO personne VALUES
(NULL, 'DENG', 'Marie', 19, '2023-04-12 20:18:58', 'CFA INSTA', 2),
(NULL, 'DENG', 'Ritsmey', 22, '2023-04-12 20:47:20', 'CFA INSTA - Compta', 1),
(NULL, 'DENG', 'Phoungeun', 48, '2023-04-12 21:30:09', NULL, NULL);

CREATE TABLE contact (
	id_contact int(11) auto_increment NOT NULL,
	id_personne int(11) NOT NULL,
	nom varchar(50) DEFAULT NULL,
	prenom varchar(50) DEFAULT NULL,
	age int(3) DEFAULT NULL,
	date_enregistrement datetime DEFAULT NULL,
	ecole varchar(50) DEFAULT NULL,
	nbannee int(11) DEFAULT NULL,
	primary key(id_contact)
);

//trigger
delimiter $
create trigger contact_sauvegarde after insert on personne
for each row 
begin 
	insert into contact values (null, new.id_personne, new.nom, new.prenom, new.age, new.date_enregistrement, new.ecole, new.nbannee);
end $
delimiter ;

insert into personne values (null, 'CHONG', 'Alice', 19, NOW(), NULL, NULL);

//procedure
delimiter  $
create procedure insert_personne (IN p_nom varchar(50), IN p_prenom varchar(50), IN p_age int(3), IN p_date_enregistrement datetime, IN p_ecole varchar(50), IN p_nbannee int(11))
begin
	insert into personne values (null, p_nom, p_prenom, p_age, p_date_enregistrement, p_ecole, p_nbannee);
end $
delimiter  ;

call insert_personne('LIAV', 'Emmanuelle', 19, NOW(), 'distance', 2);

8. Utilisation PHP + Mysql + PDO.

<?php
	echo('Normal<br/>');
	$ouverture = mysqli_connect ('localhost','root','','test2');
	$requete="SELECT* from personne";
	$execution = mysqli_query($ouverture,$requete);
	while($unepersonne = mysqli_fetch_assoc ($execution)){
		echo($unepersonne['id_personne'].' '.$unepersonne['nom'].' '.$unepersonne['prenom'].' '.$unepersonne['age'].' '.$unepersonne['date_enregistrement'].' '.$unepersonne['ecole'].' '.$unepersonne['nbannee'].'<br/>');
	}
	mysqli_close($ouverture);
	
	echo('<br/>PDO<br/>');
	$server = "localhost";
	$bdd = "test2";
	$user = "root";
	$mdp = "";
	$saisie = "R";
	//$saisie = "toto' or 1='1";(malveillance injection SQL)
	$ouverture = new PDO("mysql:host=".$server.";dbname=".$bdd, $user, $mdp);
	$requete = "SELECT * from personne where prenom like '".$saisie."%';";
	//echo($requete."<br/>");
	$select = $ouverture->prepare($requete);
	$select->execute();
	$lesPersonnes = $select->fetchAll();
	foreach ($lesPersonnes as $unepersonne) {
		echo($unepersonne['id_personne'].' '.$unepersonne['nom'].' '.$unepersonne['prenom'].' '.$unepersonne['age'].' '.$unepersonne['date_enregistrement'].' '.$unepersonne['ecole'].' '.$unepersonne['nbannee'].'<br/>');
	}
	$ouverture = null;
	
	echo('<br/>PDO (requête préparée)<br/>');
	$server = "localhost";
	$bdd = "test2";
	$user = "root";
	$mdp = "";
	$saisie = "toto' or 1='1";
	$ouverture = new PDO("mysql:host=".$server.";dbname=".$bdd, $user, $mdp);
	$requete = "SELECT * from personne where prenom like :saisie";
	$donnees = array (":saisie" => $saisie."%");
	$select = $ouverture->prepare($requete);
	$select->execute($donnees);
	$lesPersonnes = $select->fetchAll();
	foreach ($lesPersonnes as $unepersonne) {
		echo($unepersonne['id_personne'].' '.$unepersonne['nom'].' '.$unepersonne['prenom'].' '.$unepersonne['age'].' '.$unepersonne['date_enregistrement'].' '.$unepersonne['ecole'].' '.$unepersonne['nbannee'].'<br/>');
	}
	$ouverture = null;
	
	echo('<br/>PDO (procedure préparée)<br/>');
	$server = "localhost";
	$bdd = "test2";
	$user = "root";
	$mdp = "";
	$nom = "CARBON";
	$prenom = "Julia";
	$age = 32;
	$date_enregistrement = "2023-05-08";
	$ecole = "CFA-INSTA";
	$nbannee = 2;
	$ouverture = new PDO("mysql:host=".$server.";dbname=".$bdd, $user, $mdp);
	$requete = "call insert_personne(:nom, :prenom, :age, :date_enregistrement, :ecole, :nbannee);";
	$donnees = array (":nom" => $nom,
					":prenom" => $prenom,
					":age" => $age,
					":date_enregistrement" => $date_enregistrement,
					":ecole" => $ecole,
					":nbannee" => $nbannee
	);
	$select = $ouverture->prepare($requete);
	$select->execute($donnees);
	$ouverture = null;
?>