<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Tue, 28 Apr 2026 00:50:30 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Derniers articles - STHDA : PHP]]></title>
		<atom:link href="https://www.sthda.com/french/syndication/rss/articles/20" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Derniers articles - STHDA : PHP]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>fr</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Site Web avec PHP et MySQL]]></title>
			<link>https://www.sthda.com/french/articles/20-php/55-site-web-avec-php-et-mysql/</link>
			<guid>https://www.sthda.com/french/articles/20-php/55-site-web-avec-php-et-mysql/</guid>
			<description><![CDATA[[page]Programmes n?cessaires[/page]<br />
<br />
<!-- START HTML -->
<ul id="toc"></ul>
<!-- END HTML --><br />
<br />
<strong>Apache</strong> : Serveur web d?livrant les pages web aux visiteurs<br />
<strong>PHP</strong> : Plugin Apache traitant les pages dynamiques<br />
<strong>MySQL</strong> : Logiciel de gestion des bases de donn?es<br />
<br />
Nul besoin d'installer ces progammes individuellement. Des packs existent regroupant Apache + PHP + MySQL.<br />
<br />
<h3 class="formatter-title">Sous windows : Installer WAMP </h3><br />
<br />
<a href="http://telecharger.tomsguide.fr/WAMP5,0301-4008.html">Cliquez ici</a> pour l'installation.<br />
<br />
Lorsque vous d?marrer WAMP, une ic?ne apparait dans la barre des taches.<br />
<br />
WAMP est par d?faut en anglais. Pour changer la langue, click droit -> langage -> french<br />
<br />
Cr?ez un projet web dans c:/wamp/www et nommer le test.<br />
<br />
Pour lancer la page d'accueil : click gauche -> localhost. Votre projet devrait ?tre pr?sent dans la section projet.<br />
<br />
<h3 class="formatter-title">Sous MACOSX : Installer MAMP </h3><br />
<br />
<a href="http://www.mamp.info/en/index.html">Cliquez ici</a> pour l'installation.<br />
<br />
Vous avez la possibilt? de configurer le r?pertoire dans lequel Apache ira chercher les fichiers PHP de votre site web.<br />
<br />
D?marrez MAMP -> Bouton pr?f?rences -> Onglet Apache ->choisir->r?pertoire du site<br />
<br />
Vous pouvez visualiser le contenu de votre site en vous rendant ? l'adresse <a href="http://localhost:8888/votre_site/.">http://localhost:8888/votre_site/.</a><br />
<br />
<br />
<h3 class="formatter-title">Sous LINUX : Installer XAMP </h3><br />
<br />
<a href="http://www.apachefriends.org/fr/xampp.html">Cliquez ici</a> pour l'installation.<br />
<br />
Pour demarre XAMP, taper lacommande : /opt/lampp/lampp start<br />
<br />
Les fichiers PHP devront ?tre plac?s dans le r?pertoire /opt/lampp/htdocs. Vous pouvez y cr?er un sous-r?pertoire tests pour vos premiers tests.<br />
<br />
Vous pouvez y acc?der depuis votre navigateur ? l'adresse suivante : <a href="http://localhost/tests.">http://localhost/tests.</a><br />
<br />
<h3 class="formatter-title">Utiliser un bon ?diteur de fichiers</h3><br />
<br />
Sous Windows : <a href="http://notepad-plus-plus.org/">Notepad++</a><br />
<br />
Sous Mac OSX : <a href="http://www.barebones.com/products/TextWrangler/download.html">TextWrangler</a><br />
<br />
Sous Linux : gedit<br />
<br />
[page]Les bases de PHP[/page]<br />
<br />
<!-- START HTML -->
<ul id="toc"></ul>
<!-- END HTML --><br />
PHP est un langage utilis? pour concevoir des sites web dynamiques contenant des espaces membres, forum, des actualit?s, des newsletter etc. Vous trouverez dans cet article un m?mento des fonctions cl?s de PHP et MySQL. Cet article a ?t? cr?? ? partir des cours sur PHP  faits par Mathieu Nebra sur le site du z?ro.<br />
<br />
<h3 class="formatter-title">Structure minimale</h3><br />
<br />
Le code PHP s'?crit entre les balises :<strong><?php  VOTRE CODE ICI ?></strong>  et s'ins?rer au milieu du code HTML.<br />
<br />
<span class="notice"><br />
Pour faire des commentaires dans le code, il suffit d'utiliser // ou /* */.<br />
// : Commentaire sur une ligne<br />
/* Votre commentaire ici */ : Commentaire sur plusieurs lignes<br />
</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Canard'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Affichage d'un texte</span>
<span style="color: #0000FF; font-weight: bold;">print</span> <span style="color: #808080;">'Canard'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//idem, Affichage d'un texte</span>
<span style="color: #008000; font-style: italic;">/*Les commentaires longs ici . */</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning">Notez bien que chaque ligne de code se termine avec un point-virgule ";"</span><br />
<br />
<h3 class="formatter-title">Les variables</h3><br />
<br />
Les variables permettent de garder de l'information en m?moire. <strong>Le nom d'une variable est toujours pr?c?d? par le symbole $</strong><br />
<br />
 <h4 class="formatter-title">Types de variables</h4> <br />
5 types de variables existent :<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$mon_age</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">25</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Un entier (type int)</span>
<span style="color: #000080;">$mon_nom</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">"pearson"</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//cha?ne de caract?re (type string)</span>
<span style="color: #000080;">$mon_poids</span><span style="color: #8000FF;">=</span><span style="color:#800080;">60.5</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Nombre d?cimal (type float)</span>
<span style="color: #008000; font-style: italic;">//type bool?en (pour dire si une variable est vrai ou pas)</span>
<span style="color: #000080;">$je_suis_majeur</span> <span style="color: #8000FF;">=</span> <span style="">true</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//bool</span>
<span style="color: #000080;">$je_suis_mineur</span><span style="color: #8000FF;">=</span><span style="">false</span><span style="color: #8000FF;">;</span><span style="color: #008000; font-style: italic;">//bool</span>
<span style="color: #000080;">$pas_de_valeur</span><span style="color: #8000FF;">=</span><span style="">NULL</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Variable vide</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Affichage et concat?nation des variables</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$mon_age</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">25</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'mon ?ge est : '</span><span style="color: #8000FF;">.</span><span style="color: #000080;">$mon_age</span><span style="color: #8000FF;">.</span><span style="color: #808080;">' ans'</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success">Ce code affichera : "mon ?ge est 25 ans"</span><br />
<br />
 <h4 class="formatter-title">Calculs simples</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Calculs simples</span>
<span style="color: #008000; font-style: italic;">// $resultat prend ? chaque fois le r?sultat de l'op?ration</span>
<span style="color: #000080;">$resultat</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">10</span><span style="color: #8000FF;">+</span><span style="color: #FF8000;">5</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Addition</span>
<span style="color: #000080;">$resultat</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">10</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">5</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Multiplication</span>
<span style="color: #000080;">$resultat</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">10</span><span style="color: #8000FF;">/</span><span style="color: #FF8000;">5</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Division</span>
<span style="color: #000080;">$resultat</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">10</span><span style="color: #8000FF;">-</span><span style="color: #FF8000;">5</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//Soustraction</span>
<span style="color: #000080;">$resultat</span><span style="color: #8000FF;">=</span><span style="color: #8000FF;">&#40;</span><span style="color: #FF8000;">10</span><span style="color: #8000FF;">-</span><span style="color: #FF8000;">5</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">2</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//Plus complexe</span>
<span style="color: #000080;">$nombre</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">3</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$resultat</span> <span style="color: #8000FF;">=</span><span style="color: #000080;">$nombre</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">6</span><span style="color: #8000FF;">;</span><span style="color: #008000; font-style: italic;">//$resultat prend la valeur 18</span>
<span style="color: #008000; font-style: italic;">//Le modulo</span>
<span style="color: #000080;">$resultat</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">9</span><span style="color: #8000FF;">%</span><span style="color:#800080;">4</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//$resultat prend comme valeur le reste de la division, c'est ? dire 1 dans ce cas pr?cis</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<h3 class="formatter-title">Les conditions</h3><br />
<br />
 <h4 class="formatter-title">Les symboles</h4> <br />
<span class="warning"><br />
Avant de commencer, les symboles ? connaitre : <br />
== : Est ?gal ?;  > : Est sup?rieur ?; < : Est inf?rieur ?; >= : Est sup?rieur ou ?gal ?; <= : Est inf?rieur ou ?gal ?; != : Est diff?rent de;<br />
</span><br />
<br />
<span class="notice">Notez que dans les conditions on utilisera toujours la double ?galit?.</span><br />
<br />
 <h4 class="formatter-title">if...else</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #008000; font-style: italic;">//cas des entiers</span>
<span style="color: #000080;">$age</span><span style="color: #8000FF;">=</span><span style="color: #FF8000;">25</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$age</span> <span style="color: #8000FF;"><</span><span style="color: #FF8000;">18</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'vous ?tes mineur'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">elseif</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$age</span><span style="color: #8000FF;">==</span><span style="color: #FF8000;">18</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Bravo!!'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'vous ?tes majeurs'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//cas des chaines de caract?res</span>
<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nom</span><span style="color: #8000FF;">==</span><span style="color: #808080;">"pearson"</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Bienvenue administrateur!!'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Bienvenue visiteur'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//cas des bool?ens</span>
<span style="color: #000080;">$majeur</span><span style="color: #8000FF;">=</span><span style="">false</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$majeur</span><span style="color: #8000FF;">==</span><span style="">true</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Autorisation accord?e'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">else</span><span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Vous n\'?tes pas autoris?s'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;</pre></pre></div><br />
<br />
<strong><span style="color:blue;">Conditions multiples</span></strong><br />
<br />
On souhaite poser plusieurs conditions ? la fois. Les principaux mots cl?s ? connaitre sont :<br />
<strong>AND </strong>: symbole ?quivalent( && ); signification (et)<br />
<strong>OR</strong> : symbole ?quivalent( || ); signification (ou)<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//AND</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$age</span> <span style="color: #8000FF;">>=</span><span style="color: #FF8000;">30</span> <span style="color: #8000FF;">&&</span> <span style="color: #000080;">$sexe</span> <span style="color: #8000FF;">==</span> <span style="color: #808080;">"homme"</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"Bonjour Monsieur"</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//OR</span>
<span style="color: #0000FF; font-weight: bold;">if</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$pays</span><span style="color: #8000FF;">==</span><span style="color: #808080;">"France"</span> <span style="color: #8000FF;">||</span> <span style="color: #000080;">$pays</span><span style="color: #8000FF;">==</span><span style="color: #808080;">"Espagne"</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"salut Europ?en"</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">switch</h4> <br />
<br />
switch est une alternative ? if...else pour ?viter une structure longue et r?p?titive.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$note</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">10</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">switch</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$note</span><span style="color: #8000FF;">&#41;</span> <span style="color: #008000; font-style: italic;">// on indique sur quelle variable on travaille</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">case</span> <span style="color: #FF8000;">0</span><span style="color: #8000FF;">:</span> <span style="color: #008000; font-style: italic;">// dans le cas o? $note vaut 0</span>
        <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"Tu es vraiment null!!!"</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">break</span><span style="color: #8000FF;">;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">case</span> <span style="color: #FF8000;">10</span><span style="color: #8000FF;">:</span> 
        <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"Passable"</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">break</span><span style="color: #8000FF;">;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">case</span> <span style="color: #FF8000;">20</span><span style="color: #8000FF;">:</span>
        <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"Excellent travail!"</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">break</span><span style="color: #8000FF;">;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">default</span><span style="color: #8000FF;">:</span>
        <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"D?sol?, je n'ai pas de mention ? afficher pour cette note"</span><span style="color: #8000FF;">;</span><span style="color: #008000; font-style: italic;">//message par d?faut</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Les ternaires : conditions condens?es</h4> <br />
<br />
Dans l'exemple ci-dessous, si l'age est sup?rieur ? 18 alors la variable $majeur prend la valeur true.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$age</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">24</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #000080;">$majeur</span> <span style="color: #8000FF;">=</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$age</span> <span style="color: #8000FF;">>=</span> <span style="color: #FF8000;">18</span><span style="color: #8000FF;">&#41;</span> ? <span style="">true</span> <span style="color: #8000FF;">:</span> <span style="">false</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Boucle while</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Ecrire le num?ro de ligne 100 fois</span>
<span style="color: #000080;">$nombre_de_lignes</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">1</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">while</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nombre_de_lignes</span> <span style="color: #8000FF;"><=</span> <span style="color: #FF8000;">100</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Ligne numero '</span><span style="color: #8000FF;">.</span><span style="color: #000080;">$nombre_de_lignes</span><span style="color: #8000FF;">.</span><span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
    <span style="color: #008000; font-style: italic;">//Le compteur de lignes est incr?ment? ? chaque fois</span>
    <span style="color: #000080;">$nombre_de_lignes</span><span style="color: #8000FF;">++;</span> <span style="color: #008000; font-style: italic;">// $nombre_de_lignes = $nombre_de_lignes + 1</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<span class="notice"><br />
La boucle pose la condition : <strong>TANT QUE</strong> $nombre_de_lignes est inf?rieur ou ?gal ? 100.<br />
</span><br />
<br />
 <h4 class="formatter-title">Boucle for</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">for</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nombre_de_lignes</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">1</span><span style="color: #8000FF;">;</span> <span style="color: #000080;">$nombre_de_lignes</span> <span style="color: #8000FF;"><=</span> <span style="color: #FF8000;">100</span><span style="color: #8000FF;">;</span> <span style="color: #000080;">$nombre_de_lignes</span><span style="color: #8000FF;">++</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Ligne n?'</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$nombre_de_lignes</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success"><br />
3 ?l?ments interviennent dans la boucle for. Le premier sert ? <strong>l'initialisation</strong>. Le second, c'est la <strong>condition</strong>. Enfin, le troisi?me c'est l'<strong>incr?mentation</strong>.<br />
</span><br />
<br />
<h3 class="formatter-title">Les tableaux</h3><br />
<br />
 <h4 class="formatter-title">Tableaux num?rot?s</h4> <br />
<br />
<span class="warning"> La fonction array permet de cr?er un array (tableau). La num?rotation des ?l?ments d'un tableau commence par z?ro.</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Methode 1</span>
<span style="color: #000080;">$prenoms</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Michel'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Nicole'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'V?ronique'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Beno?t'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//M?thode 2 : Cr?ation manuelle</span>
<span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #FF8000;">0</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #FF8000;">1</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Michel'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//M?thode 3 : PHP affecte automatiquement les num?ros de case</span>
<span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Cr?era $prenoms[0]</span>
<span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Michel'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Cr?era $prenoms[1]</span>
<span style="color: #008000; font-style: italic;">//Afficher un tableau num?rot?</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #FF8000;">1</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Tableaux associatifs</h4> <br />
<br />
Dans le cas d'un tableau associatif, les cases ne sont pas num?rot?es mais plut?t ?tiquet?es.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">// On cr?e notre array $coordonnees</span>
<span style="color: #008000; font-style: italic;">//M?thode 1</span>
<span style="color: #000080;">$coordonnees</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span>
    <span style="color: #808080;">'prenom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Dupont'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'adresse'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'3 Rue du Paradis'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'ville'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Marseille'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//M?thode 2</span>
<span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prenom'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Dupont'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'adresse'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'3 Rue du Paradis'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'ville'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'Marseille'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//Afficher un tableau associatif</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'ville'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Parcourir un tableau</h4> <br />
<br />
Trois moyens d'explorer un tableau : for, foreach, print_r.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">// On cr?e notre array $prenoms</span>
<span style="color: #000080;">$prenoms</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Michel'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Nicole'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'V?ronique'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Beno?t'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// Boucle for pour tout afficher :</span>
<span style="color: #0000FF; font-weight: bold;">for</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$numero</span> <span style="color: #8000FF;">=</span> <span style="color: #FF8000;">0</span><span style="color: #8000FF;">;</span> <span style="color: #000080;">$numero</span> <span style="color: #8000FF;"><</span> <span style="color: #FF8000;">5</span><span style="color: #8000FF;">;</span> <span style="color: #000080;">$numero</span><span style="color: #8000FF;">++</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$prenoms</span><span style="color: #8000FF;">&#91;</span><span style="color: #000080;">$numero</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// affichera $prenoms[0], $prenoms[1] etc.</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//Boucle foreach</span>
<span style="color: #0000FF; font-weight: bold;">foreach</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$prenoms</span> <span style="color: #0000FF; font-weight: bold;">as</span> <span style="color: #000080;">$element</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$element</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// affichera $prenoms[0], $prenoms[1] etc.</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//affiche rapidement un tableau avec print_r</span>
<span style="color: #008000; font-style: italic;">//utile pour le d?bogage lors de la conception d'un site</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'<pre>'</span><span style="color: #8000FF;">;</span>
<a href="http://www.php.net/print_r"><span style="color: #0000FF; font-weight: bold;">print_r</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'</pre>'</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success">L'avantage de foreach, c'est qu'il permet de parcourir ?galement les tableaux associatifs.</span><br />
<br />
<span style="color:blue;"><strong>Exemple:</strong></span><br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$coordonnees</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span>
    <span style="color: #808080;">'prenom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Dupont'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'adresse'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'3 Rue du Paradis'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'ville'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Marseille'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">foreach</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$coordonnees</span> <span style="color: #0000FF; font-weight: bold;">as</span> <span style="color: #000080;">$element</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$element</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//On peut aussi r?cup?rer la cl? de l'?l?ment</span>
<span style="color: #0000FF; font-weight: bold;">foreach</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$coordonnees</span> <span style="color: #0000FF; font-weight: bold;">as</span> <span style="color: #000080;">$cle</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$element</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'['</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$cle</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'] vaut '</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$element</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span> 
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Rechercher dans un tableau</h4> <br />
<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span> 
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #008000; font-style: italic;">//V?rifier si une cl? existe dans l'array : array_key_exists</span>
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #000080;">$coordonnees</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span>
    <span style="color: #808080;">'prenom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Fran?ois'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nom'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Dupont'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'adresse'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'3 Rue du Paradis'</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'ville'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Marseille'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array_key_exists"><span style="color: #0000FF; font-weight: bold;">array_key_exists</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$coordonnees</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'La cl? "nom" se trouve dans les coordonn?es !'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #008000; font-style: italic;">//V?rifier si une valeur existe dans l'array : in_array</span>
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #000080;">$fruits</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a> <span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Banane'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Pomme'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Poire'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Cerise'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Fraise'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Framboise'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #0000FF; font-weight: bold;">in_array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Myrtille'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$fruits</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'La valeur "Myrtille" se trouve dans les fruits !'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #008000; font-style: italic;">//R?cup?rer la cl? d'une valeur dans l'array : array_search</span>
<span style="color: #008000; font-style: italic;">//********************************************************************</span>
<span style="color: #000080;">$position</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array_search"><span style="color: #0000FF; font-weight: bold;">array_search</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Fraise'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$fruits</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'"Fraise" se trouve en position '</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$position</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<span class="warning"><br />
N'oubliez pas qu'un array num?rot? commence ? 0 !  ? Fraise ? se trouve donc en position 4.</span><br />
<br />
[page]Transmettre des donn?es de page en page[/page]<br />
<!-- START HTML -->
<ul id="toc"></ul>
<!-- END HTML --><br />
<br />
<h3 class="formatter-title">Envoyer des donn?es dans l'URL</h3><br />
<br />
 <h4 class="formatter-title">Envoie des donn?es</h4> <br />
Soit deux pages <strong>index.php</strong> et <strong>bonjour.php</strong>.  La page index.php envoie les variables nom et pr?nom ? la page bonjour.php. Cette derni?re les r?cup?re et affiche bonjour ? la personne.<br />
<br />
<strong>Il suffit de cr?er un lien avec des param?tres qui m?ne vers la page bonjour de la forme suivante :<br />
page.php?param1=valeur1?m2=valeur2?m3=valeur3?m4=valeur4?</strong><br />
<br />
<span class="notice">Il n'est pas conseill? de d?passer les 256 caract?res</span><br />
<br />
Ouvrez index.php et ins?rez-y par exemple le code suivant :<br />
<br />
<span class="formatter-code">Code HTML :</span><div class="code"><pre style="display:inline;"><pre class="html" style="font-family:monospace;"><span style="color: #009900;"><<a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"bonjour.php?nom=Dupont&prenom=Jean"</span>></span>Dis-moi bonjour !<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">a</span></a>></span></pre></pre></div><br />
<br />
 <h4 class="formatter-title">R?cup?ration des donn?es</h4> <br />
<br />
<span class="warning">La commande <strong>$_GET</strong> permet de r?cup?rer les donn?es. On va d'abord tester l?existence des param?tres avant de les r?cup?rer au cas o? le visiteur aurait trafiquer l'URL</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//La commande isset teste l'existence des param?tres</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prenom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span> AND <a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #008000; font-style: italic;">// On a le nom et le pr?nom</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Bonjour '</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prenom'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">' '</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">' !'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #008000; font-style: italic;">// Il manque des param?tres, on avertit le visiteur</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Il faut renseigner un nom et un pr?nom !'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<h3 class="formatter-title">Envoyer des donn?es via un formulaire</h3><br />
<br />
 <h4 class="formatter-title">Base du formulaire</h4> <br />
<br />
<span class="formatter-code">Code HTML :</span><div class="code"><pre style="display:inline;"><pre class="html" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><<a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"post"</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"page_cible.php"</span>></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
    On ins?rera ici les ?l?ments de notre formulaire.
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a>></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning">Les donn?es seront r?cup?r?es gr?ce ? la fonction $_POST.</span><br />
<br />
 <h4 class="formatter-title">Les ?l?ments du formulaire</h4> <br />
<br />
<span class="formatter-code">Code HTML :</span><div class="code"><pre style="display:inline;"><pre class="html" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;"><!--//**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//champ de texte</span>
<span style="color: #808080; font-style: italic;">//name(obligatoire)  : La valeur du champ r?cup?r?e via $_POST['pseudo']</span>
<span style="color: #808080; font-style: italic;">//value(facultatif) : valeur par d?faut du champ</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"text"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"pseudo"</span>  <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"pearson"</span><span style="color: #66cc66;">/</span>></span>
<span style="color: #808080; font-style: italic;"><!--//**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//Les grandes zones de texte</span>
<span style="color: #808080; font-style: italic;">//R?cup?ration du contenu via $_POST['message']</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/textarea.html"><span style="color: #000000; font-weight: bold;">textarea</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"message"</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"8"</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"45"</span>></span>
Votre message ici.
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/textarea.html"><span style="color: #000000; font-weight: bold;">textarea</span></a>></span>
<span style="color: #808080; font-style: italic;"><!--//**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//La liste d?roulante</span>
<span style="color: #808080; font-style: italic;">//Une variable $_POST['choix'] contenant le choix de l'utilisateur est cr??. </span>
<span style="color: #808080; font-style: italic;">//S'il a choisi ? Choix 3 ?, la variable $_POST['choix'] sera ?gale au value correspondant, c'est-?-dire choix3.</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/select.html"><span style="color: #000000; font-weight: bold;">select</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"choix"</span>></span>
    <span style="color: #009900;"><<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"choix1"</span>></span>Choix 1<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>></span>
    <span style="color: #009900;"><<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"choix2"</span> <span style="color: #000066;">selected</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"selected"</span>></span>Choix 2<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>></span>//choix par d?faut gr?ce ? l'option selected
    <span style="color: #009900;"><<a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"choix3"</span>></span>Choix 3<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/option.html"><span style="color: #000000; font-weight: bold;">option</span></a>></span>
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/select.html"><span style="color: #000000; font-weight: bold;">select</span></a>></span>
<span style="color: #808080; font-style: italic;"><!--//**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//Case ? cocher</span>
<span style="color: #808080; font-style: italic;">//la variable $_POST['case'] est cr??e dans la page cible. Si elle est coch?e, sa valeur est ?gale "on"; sinon elle n'existe pas</span>
<span style="color: #808080; font-style: italic;">//Pour cocher une case par d?faut : mettre l'attribut checked="checked"</span>
<span style="color: #808080; font-style: italic;">// Faire un test avec isset($_POST['case']) pour v?rifier si la case a ?t? coch?e ou non.</span>
<span style="color: #808080; font-style: italic;">**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"checkbox"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"case"</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"case"</span> <span style="color: #66cc66;">/</span>></span> <span style="color: #009900;"><<a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"case"</span>></span>case<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a>></span> 
<span style="color: #808080; font-style: italic;"><!--**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//Bouton d?options</span>
<span style="color: #808080; font-style: italic;">//Pour pr?-cocher une case par d?faut : mettre l'attribut checked="checked"</span>
<span style="color: #808080; font-style: italic;">//Variable cr??e $_POST['frites']. Elle aura la valeur du bouton d'option choisi par le visiteur, issue de l'attribut value. </span>
<span style="color: #808080; font-style: italic;">//Si on aime les frites, alors on aura $_POST['frites'] = 'oui'.</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
Aimez-vous les frites ?
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"radio"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"frites"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"oui"</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"oui"</span> <span style="color: #000066;">checked</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"checked"</span> <span style="color: #66cc66;">/</span>></span> <span style="color: #009900;"><<a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"oui"</span>></span>Oui<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a>></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"radio"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"frites"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"non"</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"non"</span> <span style="color: #66cc66;">/</span>></span> <span style="color: #009900;"><<a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"non"</span>></span>Non<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/label.html"><span style="color: #000000; font-weight: bold;">label</span></a>></span>
<span style="color: #808080; font-style: italic;"><!--**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//Les champs cach?s</span>
<span style="color: #808080; font-style: italic;">//Le champ n'est pas affich? aux visiteurs mais il est envoy? ? la page cible lors de la validation du formulaire</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"hidden"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"pseudo"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"pearson"</span> <span style="color: #66cc66;">/</span>></span>
<span style="color: #808080; font-style: italic;"><!--**********************************************************************************************</span>
<span style="color: #808080; font-style: italic;">//Bouton d?envois du formulaire</span>
<span style="color: #808080; font-style: italic;">//**********************************************************************************************--></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"submit"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"Valider"</span> <span style="color: #66cc66;">/</span>></span>
&nbsp;</pre></pre></div><br />
<br />
<!-- START HTML -->

<div class="highlight_3d" style="width:95%; height:370px;">
<b>champ de texte</b>
<input type="text" name="pseudo"  value="pearson"/>
<br/><br/>
<b>Les grandes zones de texte</b>
<textarea name="message" rows="8" cols="45">
Votre message ici.
</textarea>
<br/><br/>
<b>La liste d?roulante</b>
<select name="choix">
    <option value="choix1">France</option>
    <option value="choix2" selected="selected">Espagne</option>//choix par d?faut gr?ce ? l'option selected
    <option value="choix3">Italie</option>
</select>
<br/><br/>
<b>Case ? cocher</b><br/>
<input type="checkbox" name="frite" id="frite" /> <label for="frite">Frites</label> <br/>
<input type="checkbox" name="choux" id="choux" checked="checked" /> <label for="choux">Choux</label> <br/>
<input type="checkbox" name="banane" id="banane"  /> <label for="banane">Banane</label> 
<br/><br/>
<b>Bouton d'options</b>
Aimez-vous les frites ?
<input type="radio" name="frites" value="oui" id="oui" checked="checked" /> <label for="oui">Oui</label>
<input type="radio" name="frites" value="non" id="non" /> <label for="non">Non</label>
<br/><br/>
<b>Bouton valider</b>
<input type="submit" value="Valider" />
</div>

<!-- END HTML --><br />
<br />
<br />
 <h4 class="formatter-title">R?cup?ration des donn?es</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span> 
<span style="color: #008000; font-style: italic;">//?chappement des balises html</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <a href="http://www.php.net/htmlspecialchars"><span style="color: #0000FF; font-weight: bold;">htmlspecialchars</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prenom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> 
<span style="color: #008000; font-style: italic;">//suppression des balises html</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <a href="http://www.php.net/strip_tags"><span style="color: #0000FF; font-weight: bold;">strip_tags</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prenom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
Notez l'utilisation de la fonction <strong>htmlspecialchars</strong>. Elle permet d'?chapper les balises HTML ins?r?es par le visiteurs prot?geant ainsi votre site d'?ventuelles attaques. Les balises HTML seront affich?es mais non ex?cut?es.<br />
</span><br />
<br />
<span class="success"><br />
Si vous pr?f?rez retirer les balises HTML que le visiteur a tent? d'envoyer plut?t que de les afficher, utilisez la fonction <strong>strip_tags</strong>.<br />
</span><br />
<br />
<h3 class="formatter-title">Envoyer un fichier </h3><br />
<br />
 <h4 class="formatter-title">Formulaire d'envois</h4> <br />
<br />
<span class="notice">Si le formulaire propose aux visiteurs d'envoyer un fichier, il faut ajouter l'attribut enctype="multipart/form-data" ? la balise <form>.</span><br />
<br />
<span class="formatter-code">Code HTML :</span><div class="code"><pre style="display:inline;"><pre class="html" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><<a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"page_cible.php"</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"post"</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"multipart/form-data"</span>></span>
        <span style="color: #009900;"><<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
                Formulaire d'envoi de fichier :<span style="color: #009900;"><<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>></span>
                <span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"file"</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"monfichier"</span> <span style="color: #66cc66;">/</span>><<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>></span>
                <span style="color: #009900;"><<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"submit"</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"Envoyer le fichier"</span> <span style="color: #66cc66;">/</span>></span>
        <span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a>></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Traitement de l'envoi</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">// Testons si le fichier a bien ?t? envoy? et s'il n'y a pas d'erreur</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span> AND <span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'error'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">==</span> <span style="color: #FF8000;">0</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// Testons si le fichier n'est pas trop gros</span>
        <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'size'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;"><=</span> <span style="color: #FF8000;">1000000</span><span style="color: #8000FF;">&#41;</span> <span style="color: #008000; font-style: italic;">// en octet; 1 MO = 1000 000 octets</span>
        <span style="color: #8000FF;">&#123;</span>
                <span style="color: #008000; font-style: italic;">// Testons si l'extension est autoris?e</span>
                <span style="color: #000080;">$infosfichier</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/pathinfo"><span style="color: #0000FF; font-weight: bold;">pathinfo</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'name'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
                <span style="color: #000080;">$extension_upload</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$infosfichier</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'extension'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
                <span style="color: #000080;">$extensions_autorisees</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'jpg'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'jpeg'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'gif'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'png'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
                <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #0000FF; font-weight: bold;">in_array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$extension_upload</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$extensions_autorisees</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>
                <span style="color: #8000FF;">&#123;</span>
                        <span style="color: #008000; font-style: italic;">// On peut valider le fichier et le stocker d?finitivement</span>
                        <a href="http://www.php.net/move_uploaded_file"><span style="color: #0000FF; font-weight: bold;">move_uploaded_file</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'tmp_name'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'uploads/'</span> <span style="color: #8000FF;">.</span> <a href="http://www.php.net/basename"><span style="color: #0000FF; font-weight: bold;">basename</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_FILES</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'monfichier'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'name'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
                        <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">"L'envoi a bien ?t? effectu? !"</span><span style="color: #8000FF;">;</span>
                <span style="color: #8000FF;">&#125;</span>
        <span style="color: #8000FF;">&#125;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success"><br />
Le fichier est plac? dans le dossier uploads. V?rifiez que le dossier upload existe sur le serveur et qu'il a les droits d'?criture (CHMOD 733)<br />
</span><br />
<br />
<span class="error"><br />
Ce script est encore ? am?liorer.  Par exemple, si le nom du fichier contient des espaces ou des accents, ?a posera un probl?me une fois envoy? sur le Web. D'autre part, si quelqu'un envoie un fichier qui a le m?me nom que celui d'une autre personne, l'ancien sera ?cras? !<br />
La solution consiste en g?n?ral ? ? choisir ? nous-m?mes le nom du fichier stock? sur le serveur plut?t que de se servir du nom d'origine. Vous pouvez faire un compteur qui s'incr?mente : 1.png, 2.png, 3.jpg, etc.<br />
</span><br />
<br />
<h3 class="formatter-title">Les variables superglobales, sessions et cookie</h3><br />
<br />
Les variables superglobales sont accessibles sur toutes les pages de votre site.<br />
<br />
 <h4 class="formatter-title">Les sessions</h4> <br />
<br />
Pour chaque visiteur, un num?ro de session unique est cr?? automatiquement par PHP. PHP transmet cet ID de page en page en utilisant g?n?ralement un cookie.<br />
<br />
<ul class="formatter-ul">
<li class="formatter-li"><strong>session_start()</strong> : d?marre le syst?me de sessions.
</li><li class="formatter-li"><strong>session_destroy()</strong> : ferme la session du visiteur. Cette fonction est automatiquement appel?e lorsque le visiteur ne charge plus de page de votre site pendant plusieurs minutes (c'est le timeout), mais elle peut ?tre utilis?e pour la d?connexion manuelle du visiteur.<br />
</li></ul><br />
<br />
<span class="error"><br />
Il faut appeler session_start() sur chacune de vos pages AVANT d'?crire le moindre code HTML (avant m?me la balise <!DOCTYPE> ). Si vous oubliez de lancer session_start(), vous ne pourrez pas acc?der aux variables superglobales $_SESSION<br />
</span><br />
<br />
<strong><span style="color:blue;">Exemple</span></strong><br />
<span class="formatter-code">Code HTML :</span><div class="code"><pre style="display:inline;"><pre class="html" style="font-family:monospace;">&nbsp;
<span style="color: #009900;"><?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">//</span> On d?marre la session AVANT d<span style="color: #ff0000;">'?crire du code HTML</span>
<span style="color: #009900;">session_start();</span>
&nbsp;
<span style="color: #009900;">// On s'</span>amuse ? cr?er quelques variables de session dans $_SESSION</span>
<span style="color: #009900;">$_SESSION<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'prenom'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Jean'</span>;</span>
<span style="color: #009900;">$_SESSION<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'nom'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Dupont'</span>;</span>
<span style="color: #009900;">$_SESSION<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'age'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">24</span>;</span>
<span style="color: #009900;">?></span>
&nbsp;
<span style="color: #00bbdd;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></span>
<span style="color: #009900;"><<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">"http://www.w3.org/1999/xhtml"</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">"fr"</span> ></span>
   <span style="color: #009900;"><<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>></span>
       <span style="color: #009900;"><<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>></span>Titre de ma page<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>></span>
    <span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>></span>
    <span style="color: #009900;"><<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>></span>
    <span style="color: #009900;"><<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
        Salut <span style="color: #009900;"><?php echo $_SESSION<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'prenom'</span><span style="color: #66cc66;">&#93;</span>; ?></span> !<span style="color: #009900;"><<a href="http://december.com/html/4/element/br.html"><span style="color: #000000; font-weight: bold;">br</span></a> <span style="color: #66cc66;">/</span>></span>
        Tu es ? l'accueil de mon site (index.php). Tu veux aller sur une autre page ?
    <span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>></span>
    <span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>></span>
<span style="color: #009900;"><<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Les cookies</h4> <br />
<br />
Un cookie, c'est un petit fichier que l'on enregistre sur l'ordinateur du visiteur. Ce fichier contient du texte permettant de ? retenir ? des informations sur le visiteur.<br />
<br />
<span style="color:blue;"><strong>Ecrire un cookie</strong></span><br />
<br />
On utilise la fonction PHP setcookie en lui donnant, en g?n?ral trois param?tres, dans l'ordre suivant :<br />
<br />
    le nom du cookie (ex. : pseudo) ;<br />
    la valeur du cookie (ex. : M@teo21) ;<br />
    la date d'expiration du cookie, sous forme de timestamp (ex. : 1090521508).<br />
<br />
<span class="success">Si vous voulez supprimer le cookie dans un an, il vous faudra donc ?crire : time() + 365*24*3600. Cela veut dire : timestamp actuel $+$ nombre de secondes dans une ann?e.</span><br />
<br />
La commande est la suivante pour cr?er un cookie:<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span> <a href="http://www.php.net/setcookie"><span style="color: #0000FF; font-weight: bold;">setcookie</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'pseudo'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">,</span> <a href="http://www.php.net/time"><span style="color: #0000FF; font-weight: bold;">time</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">+</span> <span style="color: #FF8000;">365</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">24</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">3600</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> 
<span style="color: #008000; font-style: italic;">//S?curiser son cookie avec le mode httpOnly; le dernier true active le httpOnly</span>
<a href="http://www.php.net/setcookie"><span style="color: #0000FF; font-weight: bold;">setcookie</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'pseudo'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">,</span> <a href="http://www.php.net/time"><span style="color: #0000FF; font-weight: bold;">time</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">+</span> <span style="color: #FF8000;">365</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">24</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">3600</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">false</span><span style="color: #8000FF;">,</span> <span style="">true</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="error"><br />
 Comme pour session_start, setcookie ne marche QUE si vous l'appelez avant tout code HTML (donc avant la balise <!DOCTYPE&gt<img src="https://www.sthda.com/french/french/images/smileys/wink.png" alt=";)" class="smiley" />.<br />
</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<a href="http://www.php.net/setcookie"><span style="color: #0000FF; font-weight: bold;">setcookie</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'pseudo'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">,</span> <a href="http://www.php.net/time"><span style="color: #0000FF; font-weight: bold;">time</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">+</span> <span style="color: #FF8000;">365</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">24</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">3600</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">false</span><span style="color: #8000FF;">,</span> <span style="">true</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On ?crit un cookie</span>
<a href="http://www.php.net/setcookie"><span style="color: #0000FF; font-weight: bold;">setcookie</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'pays'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'France'</span><span style="color: #8000FF;">,</span> <a href="http://www.php.net/time"><span style="color: #0000FF; font-weight: bold;">time</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">+</span> <span style="color: #FF8000;">365</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">24</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">3600</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">false</span><span style="color: #8000FF;">,</span> <span style="">true</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On ?crit un autre cookie...</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// Et SEULEMENT MAINTENANT, on peut commencer ? ?crire du code html</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
   <head>
       <title>Ma super page PHP</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
&nbsp;
  etc.
&nbsp;</pre></pre></div><br />
<br />
<strong><span style="color:blue;">Afficher un cookie</span></strong><br />
<br />
<span class="notice"><br />
 Il faut faire un isset pour v?rifier si le cookie existe ou non.<br />
</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<p>
    H? ! Je me souviens de toi !<br />
    Tu t'appelles <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$_COOKIE</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'pseudo'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> et tu viens de <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$_COOKIE</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'pays'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> c'est bien ?a ?
</p>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
Les cookies viennent du visiteur. Il peut les avoir modifi?s, donc soyez prudents et n'ayez pas une confiance aveugle en leur contenu !<br />
</span><br />
<br />
<strong><span style="color:blue;">Modifier un cookie existant</span></strong><br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<a href="http://www.php.net/setcookie"><span style="color: #0000FF; font-weight: bold;">setcookie</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'pays'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Chine'</span><span style="color: #8000FF;">,</span> <a href="http://www.php.net/time"><span style="color: #0000FF; font-weight: bold;">time</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">+</span> <span style="color: #FF8000;">365</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">24</span><span style="color: #8000FF;">*</span><span style="color: #FF8000;">3600</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">null</span><span style="color: #8000FF;">,</span> <span style="">false</span><span style="color: #8000FF;">,</span> <span style="">true</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;</pre></pre></div><br />
<br />
<br />
<br />
[page]Lire et ?crire dans un fichier[/page]<br />
<br />
Pour ?crire dans un dossier PHP doit avoir les droits. Gr?ce ? votre logicel FTP (ie:Filezilla) vous pouvez changer les permissions des fichiers et dossiers.<br />
<br />
<strong><span style="color:blue;">Pour lire ou ?crire dans un fichier, il faut d'abord l'ouvrir:</span></strong><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">// 1 : on ouvre le fichier</span>
<span style="color: #000080;">$monfichier</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/fopen"><span style="color: #0000FF; font-weight: bold;">fopen</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'compteur.txt'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'r+'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
&nbsp;
<span style="color: #000080;">$pages_vues</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/fgets"><span style="color: #0000FF; font-weight: bold;">fgets</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$monfichier</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On lit la premi?re ligne (nombre de pages vues)</span>
<span style="color: #000080;">$pages_vues</span><span style="color: #8000FF;">++;</span> <span style="color: #008000; font-style: italic;">// On augmente de 1 ce nombre de pages vues</span>
<a href="http://www.php.net/fseek"><span style="color: #0000FF; font-weight: bold;">fseek</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$monfichier</span><span style="color: #8000FF;">,</span> <span style="color: #FF8000;">0</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On remet le curseur au d?but du fichier</span>
<a href="http://www.php.net/fputs"><span style="color: #0000FF; font-weight: bold;">fputs</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$monfichier</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$pages_vues</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On ?crit le nouveau nombre de pages vues</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// 3 : quand on a fini de l'utiliser, on ferme le fichier</span>
<a href="http://www.php.net/fclose"><span style="color: #0000FF; font-weight: bold;">fclose</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$monfichier</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
Plusieurs modes d'ouverture existent :<br />
<strong>r </strong>: Ouvre le fichier en lecture seule. Cela signifie que vous pourrez seulement lire le fichier.<br />
<strong>r+</strong>:Ouvre le fichier en lecture et ?criture.<br />
<strong>a </strong>: Ouvre le fichier en ?criture seule. Si le fichier n'existe pas, il est automatiquement cr??.<br />
<strong>a+</strong>:Ouvre le fichier en lecture et ?criture. Si le fichier & n'existe pas, il est cr?? automatiquement. Attention : le r?pertoire doit avoir un CHMOD ? 777 dans ce cas ! ? noter que si le fichier existe d?j?, le texte sera rajout? ? la fin.<br />
</span><br />
<br />
<br />
<br />
[page]Base de donn?es[/page]<br />
<!-- START HTML -->
<ul id="toc"></ul>
<!-- END HTML --><br />
<br />
<h3 class="formatter-title">Cr?ation d'une table avec phpMyAdmin</h3><br />
<br />
Les diff?rentes colonnes de la table sont appel?es "champ". <br />
<br />
<strong><span style="color:blue;">Les param?tres des champs sont :</span></strong><br />
    <strong>Champ </strong>: permet de d?finir le nom du champ;<br />
    <strong>Type </strong>: le type de donn?es que va stocker le champ (nombre entier, texte, date? ) ;<br />
    <strong>Taille/Valeurs</strong> : permet d'indiquer la taille maximale du champ, utile pour le type VARCHAR notamment, afin de limiter le nombre de caract?res autoris?s ;<br />
    <strong>Index</strong> : active l'indexation du champ. Ceci signifie que votre champ sera adapt? aux recherches. Le plus souvent, on utilise l'index PRIMARY sur les champs de type id ;<br />
    <strong>AUTO_INCREMENT</strong> : permet au champ de s'incr?menter tout seul ? chaque nouvelle entr?e. On l'utilise fr?quemment sur les champs de type id.<br />
<br />
<strong><span style="color:blue;">Plusieurs types de champs MySQL existent :</span></strong><br />
<br />
    <strong>INT</strong> : nombre entier ;<br />
    <strong>VARCHAR</strong> : texte court (entre 1 et 255 caract?res) ;<br />
    <strong>TEXT</strong> : long texte (on peut y stocker un roman sans probl?me) ;<br />
   <strong> DATE</strong> : date (jour, mois, ann?e).<br />
<br />
<span class="notice">Toute table doit poss?der un champ qui joue le r?le de cl? primaire. La cl? primaire permet d'identifier de mani?re unique une entr?e dans la table. En g?n?ral c'est le champ "id".</span><br />
<br />
<br />
<h3 class="formatter-title">Connexion ? la base de donn?es</h3><br />
<br />
<span class="notice">Pour cela on va utiliser MySQL. Les fonctions de l'extension mysql_ sont obsol?tes. Il faudrait donc utiliser l'extension mysqli_ ou PDO permettant de se connecter aussi bien ? MySQL que PostgreSQL ou Oracle. PDO est activ? par d?faut.</span><br />
<br />
<strong><span style="color:blue;">4 renseignement sont n?c?ssaires : </span></strong><br />
    <strong>le nom de l'h?te</strong> : c'est l'adresse de l'ordinateur o? MySQL est install? (comme une adresse IP) : localhost. <br />
    <strong>la base</strong> : c'est le nom de la base de donn?es ? laquelle vous voulez vous connecter. <br />
    <strong>le login</strong> : il permet de vous identifier.<br />
    <strong>le mot de passe</strong> : il y a des chances pour que le mot de passe soit le m?me que celui que vous utilisez pour acc?der au FTP. Renseignez-vous aupr?s de votre h?bergeur.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//connexion et test de la pr?sence d'erreur</span>
try<span style="color: #8000FF;">&#123;</span>
    <span style="color: #000080;">$bdd</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> PDO<span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'mysql:host=localhost;dbname=test'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'root'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
catch <span style="color: #8000FF;">&#40;</span>Exception <span style="color: #000080;">$e</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Erreur : '</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$e</span><span style="color: #8000FF;">-></span><span style="color: #000000;">getMessage</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="error">En cas d'erreur, PDO renvoie ce qu'on appelle une exception qui permet de ? capturer ? l'erreur. </span><br />
<br />
<h3 class="formatter-title">R?cup?rer les donn?es</h3><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
try<span style="color: #8000FF;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// On se connecte ? MySQL</span>
    <span style="color: #000080;">$bdd</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> PDO<span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'mysql:host=localhost;dbname=test'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'root'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
catch<span style="color: #8000FF;">&#40;</span>Exception <span style="color: #000080;">$e</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// En cas d'erreur, on affiche un message et on arr?te tout</span>
        <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Erreur : '</span><span style="color: #8000FF;">.</span><span style="color: #000080;">$e</span><span style="color: #8000FF;">-></span><span style="color: #000000;">getMessage</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// Si tout va bien, on peut continuer</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// On r?cup?re tout le contenu de la table jeux_video</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT * FROM jeux_video'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// On affiche chaque entr?e une ? une</span>
<span style="color: #0000FF; font-weight: bold;">while</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$donnees</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">fetch</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
    <p>
    <strong>Jeu</strong> : <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span><br />
    Le possesseur de ce jeu est : <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span>, et il le vend ? <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prix'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> euros !<br />
    Ce jeu fonctionne sur <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'console'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> et on peut y jouer ? <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nbre_joueurs_max'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> au maximum<br />
    <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> a laiss? ces commentaires sur <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span> : <em><span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'commentaires'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span></em>
   </p>
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #8000FF;">&#125;</span>
 <span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">closeCursor</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Termine le traitement de la requ?te</span>
&nbsp;
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning">Vous devez effectuer cet appel ? closeCursor() chaque fois que vous avez fini de traiter le retour d'une requ?te, afin d'?viter d'avoir des probl?mes ? la requ?te suivante. </span><br />
<br />
<br />
<h3 class="formatter-title">Crit?res de s?lection : WHERE, ORDER BY, LIMIT</h3><br />
<br />
    <strong>WHERE </strong> : permet de trier les donn?es;<br />
<br />
    <strong>ORDER BY</strong> : permet d'ordonn?es les donn?es;<br />
<br />
    <strong>LIMIT</strong> : s?lection d'une partie des r?sultats (par exemple les 20 premiers).<br />
<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #008000; font-style: italic;">/************************************************************************
WHERE : Trier
************************************************************************/</span>
<span style="color: #008000; font-style: italic;">//S?lectionner tous les champs de la table jeux_video lorsque le champ possesseur est ?gal ? Patrick</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT * FROM jeux_video WHERE possesseur=\'Patrick\''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom, possesseur FROM jeux_video WHERE possesseur=\'Patrick\''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//combinaison de plusieurs conditions: AND et OR</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"SELECT * FROM jeux_video WHERE possesseur='Patrick' AND prix < 20"</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/************************************************************************
ORDER BY : Ordonner
************************************************************************/</span>
<span style="color: #008000; font-style: italic;">//Les r?sultats sont ordonn?s par prix croissants</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom, prix FROM jeux_video ORDER BY prix'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//Les r?sultats sont ordonn?s par prix d?croissants</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom, prix FROM jeux_video ORDER BY prix DESC'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/************************************************************************
LIMIT : s?lection d'une partie des r?sultats
************************************************************************/</span>
<span style="color: #008000; font-style: italic;">//S?lectionne les dix premi?res entr?es ;</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video LIMIT 0, 10'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//LIMIT 5, 10 : affiche de la sixi?me ? la quinzi?me entr?e ;</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video LIMIT 5, 10'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> 
<span style="color: #008000; font-style: italic;">/************************************************************************
Utiliser tous les crit?res en m?me temps.
ATTENTION : Il faut respecter l'ordre : WHERE, ORDER BY, LIMIT
************************************************************************/</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"SELECT nom, possesseur, console, prix FROM jeux_video WHERE console='Xbox' OR console='PS2' ORDER BY prix DESC LIMIT 0,10"</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success">Il existe aussi le mot-cl? OR qui signifie ? ou ?.</span><br />
<br />
<span class="notice">Si on avait utilis? ORDER BY sur un champ contenant du texte, le classement aurait ?t? fait par ordre alphab?tique.</span><br />
<br />
<h3 class="formatter-title">Les requ?ttes pr?par?es</h3><br />
<br />
<span class="warning">Eviter de concatener des variables dans une requ?tte. </span><br />
<br />
<strong>La mauvaise id?e : concat?nation</strong><br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video WHERE possesseur=\''</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'\''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
Le syst?me de requ?tes pr?par?es est plus s?r mais aussi plus rapide pour la base de donn?es si la requ?te est ex?cut?e plusieurs fois.<br />
<br />
Dans un premier temps, la requ?te est pr?par?e sans sa partie variable, que l'on repr?sentera par un point d'interrogation. La requ?te est ensuite ex?cut?e ? l'aide des param?tres que l'on a indiqu?s sous forme d'array:<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
try<span style="color: #8000FF;">&#123;</span>
    <span style="color: #000080;">$bdd</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> PDO<span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'mysql:host=localhost;dbname=test'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'root'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
catch<span style="color: #8000FF;">&#40;</span>Exception <span style="color: #000080;">$e</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Erreur : '</span><span style="color: #8000FF;">.</span><span style="color: #000080;">$e</span><span style="color: #8000FF;">-></span><span style="color: #000000;">getMessage</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #008000; font-style: italic;">/************************************************************************
//Exemple 1 : une seule variable
************************************************************************/</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video WHERE possesseur = ?'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/************************************************************************
//Exemple 2 : Plusieurs variables (les param?tre doivent ?tre indiqu?es dans le bon ordre)
//lister les jeux appartenant ? une personne et dont le prix ne d?passe pas une certaine somme 
************************************************************************/</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video WHERE possesseur = ? AND prix <= ?'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prix_max'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'<ul>'</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">while</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$donnees</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">fetch</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'<li>'</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">' ('</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prix'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">' EUR)</li>'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'</ul>'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">closeCursor</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/************************************************************************
//Exemple 3 : Avec des marqueurs nominatifs (dans le cas o? la requette est trop longue)
************************************************************************/</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom, prix FROM jeux_video WHERE possesseur = :possesseur AND prix <= :prixmax'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'possesseur'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'possesseur'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'prixmax'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prix_max'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<h3 class="formatter-title">Traquer les erreurs</h3><br />
<br />
<span class="warning">Prenez l'habitude de rajouter le code or die(print_r($bdd->errorInfo())) sur la m?me ligne que votre requ?te pour afficher des d?tails sur l'erreur.</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Requ?tte simple</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom FROM jeux_video'</span><span style="color: #8000FF;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/print_r"><span style="color: #0000FF; font-weight: bold;">print_r</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">errorInfo</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//Requ?tte pr?par?e</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT nom, prix FROM jeux_video WHERE possesseur = :possesseur'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'possesseur'</span> <span style="color: #8000FF;">=></span> <span style="color: #808080;">'Patrick'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'prixmax'</span> <span style="color: #8000FF;">=></span> <span style="color: #FF8000;">20</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/print_r"><span style="color: #0000FF; font-weight: bold;">print_r</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">errorInfo</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<strong><span style="color:blue;">Supposons que j'ai mal ?cris le nom du champ. L'erreur affich?e sera : </span></strong><br />
<br />
<span class="formatter-code">Code :</span><div class="code"><code><span style="color: #000000">
Unknown&nbsp;column&nbsp;'champinconnu'&nbsp;in&nbsp;'field&nbsp;list'</span>
</code></div><br />
<br />
<br />
<h3 class="formatter-title">?crire des donn?es</h3><br />
<br />
 <h4 class="formatter-title">INSERT : Ajouter des donn?es</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
try<span style="color: #8000FF;">&#123;</span>
    <span style="color: #000080;">$bdd</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> PDO<span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'mysql:host=localhost;dbname=test'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'root'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
catch<span style="color: #8000FF;">&#40;</span>Exception <span style="color: #000080;">$e</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #0000FF; font-weight: bold;">die</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Erreur : '</span><span style="color: #8000FF;">.</span><span style="color: #000080;">$e</span><span style="color: #8000FF;">-></span><span style="color: #000000;">getMessage</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// On ajoute une entr?e dans la table jeux_video</span>
<span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><a href="http://www.php.net/exec"><span style="color: #0000FF; font-weight: bold;">exec</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'INSERT INTO jeux_video(ID, nom, possesseur, console, prix, nbre_joueurs_max, commentaires) 
                                                    VALUES('</span><span style="color: #808080;">', \'Battlefield 1942\', \'Patrick\', \'PC\', 45, 50, \'2nde guerre mondiale\')'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Le jeu a bien ?t? ajout? !'</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success"><br />
On commence par les mots-cl?s INSERT INTO,  suivis ensuite par le nom de la table (ici jeux_video), puis les noms des champs dans lesquels vous souhaitez placer des informations. On inscrit VALUES suivi des valeurs ? ins?rer dans le m?me ordre que les champs que l'on a indiqu?s.<br />
<br />
Pour le premier champ (ID), les es apostrophes sont vides. C'est voulu : le champ a la propri?t? auto_increment, MySQL mettra donc le num?ro d'ID lui-m?me<br />
</span><br />
<br />
<strong><span style="color:blue;">Insertion de donn?es variables gr?ce ? une requ?te pr?par?e</span></strong><br />
<br />
Le principe est le m?me que dans le chapitre pr?c?dent<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'INSERT INTO jeux_video(nom, possesseur, console, prix, nbre_joueurs_max, commentaires) 
                                                      VALUES(:nom, :possesseur, :console, :prix, :nbre_joueurs_max, :commentaires)'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span>
    <span style="color: #808080;">'nom'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$nom</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'possesseur'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$possesseur</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'console'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$console</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'prix'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$prix</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nbre_joueurs_max'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$nbre_joueurs_max</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'commentaires'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$commentaires</span>
    <span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'Le jeu a bien ?t? ajout? !'</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<br />
 <h4 class="formatter-title">UPDATE : Modifier des donn?es</h4> <br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Le nombre de lignes modifi? est renvoy?</span>
<span style="color: #000080;">$nb_modifs</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><a href="http://www.php.net/exec"><span style="color: #0000FF; font-weight: bold;">exec</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'UPDATE jeux_video SET possesseur = \'Florent\' WHERE possesseur = \'Michel\''</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$nb_modifs</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">' entr?es ont ?t? modifi?es !'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">//===Requ?te pr?par?e==</span>
<span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'UPDATE jeux_video SET prix = :nvprix, nbre_joueurs_max = :nv_nb_joueurs WHERE nom = :nom_jeu'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/array"><span style="color: #0000FF; font-weight: bold;">array</span></a><span style="color: #8000FF;">&#40;</span>
    <span style="color: #808080;">'nvprix'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$nvprix</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nv_nb_joueurs'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$nv_nb_joueurs</span><span style="color: #8000FF;">,</span>
    <span style="color: #808080;">'nom_jeu'</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$nom_jeu</span>
    <span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">DELETE : Supprimer des donn?es</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">WHERE</span> nom<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Battlefield 1942'</span>
&nbsp;</pre></pre></div><br />
<br />
<h3 class="formatter-title">Les fonctions SQL</h3><br />
<br />
 <h4 class="formatter-title">Les fonctions scalaires : UPPER, LOWER, LENGTH, ROUND</h4> <br />
<br />
Les fonctions scalaires modifient les valeurs.<br />
<br />
<strong><span style="color:blue;">Exemple : Conversion d'un champ en majuscule</span></strong><br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT UPPER(nom) AS nom_maj FROM jeux_video'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">while</span> <span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$donnees</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">fetch</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'nom_maj'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'<br />'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;
<span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">closeCursor</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<strong><span style="color:blue;">Les autres fonctions s'utilisent de la m?me mani?re:</span></strong><br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- Conversion du champ  en majuscule</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nom_maj <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- Conversion du champ  en minuscule</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">LOWER</span><span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nom_min <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- Compte le nombre de caract?res</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> longueur_nom <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- Arrondi un nombre d?cimal</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> ROUND<span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_arrondi <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
Vous pouvez aussi r?cup?rer le contenu des autres champs comme avant sans forc?ment leur appliquer une fonction :<br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span>nom<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nom_maj<span style="color: #66cc66;">,</span> possesseur<span style="color: #66cc66;">,</span> console<span style="color: #66cc66;">,</span> prix <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video</pre></pre></div><br />
</span><br />
<br />
<br />
<span class="notice">Une liste d?taill?e des fonctions est disponible <a href="http://dev.mysql.com/doc/refman/5.0/en/functions.html">ici</a></span><br />
<br />
 <h4 class="formatter-title">Les fonctions d?agr?gats : AVG, SUM, MAX, MIN, COUNT</h4> <br />
<br />
Les fonctions d'agr?gats font des op?rations.<br />
<br />
<strong><span style="color:blue;">Exemple : calcul de la moyenne</span></strong><br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$reponse</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">query</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT AVG(prix) AS prix_moyen FROM jeux_video'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #000080;">$donnees</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">fetch</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'prix_moyen'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #000080;">$reponse</span><span style="color: #8000FF;">-></span><span style="color: #000000;">closeCursor</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="error"><br />
Vous ne devez pas r?cup?rer d'autres champs de la table quand vous utilisez une fonction d'agr?gat, contrairement ? tout ? l'heure avec les fonctions scalaires.<br />
</span><br />
<br />
<strong><span style="color:blue;">Les autres fonctions s'utilisent de la m?me mani?re:</span></strong><br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- AVG : Calcul de la moyenne</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> AVG<span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_moyen <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- SUM : Additionner les valeurs</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_total <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">WHERE</span> possesseur<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Patrick'</span>
<span style="color: #808080; font-style: italic;">-- MAX : retourner la valeur maximale</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_max <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- MIN : retourner la valeur minimale</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">MIN</span><span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_min <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #808080; font-style: italic;">-- COUNT : compter le nombre d'entr?es</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nbjeux <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nbjeux <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">WHERE</span> possesseur<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Florent'</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
Il est possible de compter le nombre de valeurs distinctes sur un champ pr?cis.<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DISTINCT</span> possesseur<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nbpossesseurs <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video</pre></pre></div><br />
</span><br />
<br />
 <h4 class="formatter-title">GROUP BY et HAVING : groupement de donn?es</h4> <br />
<br />
<strong><span style="color:blue;">GROUP BY est utilis? pour obtenir des informations int?ressantes sur des groupes de donn?es.</span></strong><br />
<br />
<span class="warning">Il faut utiliser GROUP BY en m?me temps qu'une fonction d'agr?gat, sinon il ne sert ? rien.</span><br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- Ici, on r?cup?re le prix moyen et la console, et on choisit de grouper par console</span>
<span style="color: #808080; font-style: italic;">--On a la liste des consoles et le prix moyen des jeux associ?s.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> AVG<span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_moyen<span style="color: #66cc66;">,</span> console <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> console
&nbsp;</pre></pre></div><br />
<br />
<span style="color:blue;">HAVING est utilis? pour filtrer les donn?es ? la fin des op?rations.</span><br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">--on r?cup?re uniquement la liste des consoles et leur prix moyen si ce prix moyen ne d?passe pas 10 euros.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> AVG<span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_moyen<span style="color: #66cc66;">,</span> console <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> console <span style="color: #993333; font-weight: bold;">HAVING</span> prix_moyen <span style="color: #66cc66;"><=</span> <span style="color: #cc66cc;">10</span>
<span style="color: #808080; font-style: italic;">--exemple 2 : combiner GROUP BY et HAVING</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> AVG<span style="color: #66cc66;">&#40;</span>prix<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> prix_moyen<span style="color: #66cc66;">,</span> console <span style="color: #993333; font-weight: bold;">FROM</span> jeux_video <span style="color: #993333; font-weight: bold;">WHERE</span> possesseur<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'Patrick'</span> <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> console <span style="color: #993333; font-weight: bold;">HAVING</span> prix_moyen <span style="color: #66cc66;"><=</span> <span style="color: #cc66cc;">10</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="error"><br />
HAVING ne doit s'utiliser que sur le r?sultat d'une fonction d'agr?gat. Voil? pourquoi on l'utilise ici sur prix_moyen et non sur console.<br />
</span><br />
<br />
<br />
<h3 class="formatter-title">Les dates en SQL</h3><br />
<br />
 <h4 class="formatter-title">Les champs de type date</h4> <br />
<br />
Plusieurs types de champs existent. Les plus utilis?s sont :<br />
<br />
<strong>DATE</strong> : stocke une date au format AAAA-MM-JJ (Ann?e-Mois-Jour) ;<br />
<strong>DATETIME</strong> : stocke la combinaison d'une date et d'un moment de la journ?e au format AAAA-MM-JJ HH:MM:SS. <br />
<br />
<span class="error"><br />
Lorsque vous cr?ez votre table MySQL, nommer le champ comme ceci : date_creation, ou encore date_modification. Ne jamais utiliser "date" tout court. Cela peut g?n?rer des erreur car date est une fonction MySQL.<br />
</span><br />
<br />
 <h4 class="formatter-title">Utilisation des dates</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- Affiche la liste des messages post?s le 02/04/2010 </span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'2010-04-02'</span>
<span style="color: #808080; font-style: italic;">-- Affiche la liste des messages post?s le 02/04/2010 ? 15h28min22s</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'2010-04-02 15:28:22'</span>
<span style="color: #808080; font-style: italic;">-- Affiche la liste des messages post?s apr?s le 02/04/2010 ? 15h28min22s</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #66cc66;">>=</span> <span style="color: #ff0000;">'2010-04-02 15:28:22'</span>
<span style="color: #808080; font-style: italic;">-- Affiche la liste des messages post?s entre deux dates:</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #66cc66;">>=</span> <span style="color: #ff0000;">'2010-04-02 00:00:00'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #66cc66;"><=</span> <span style="color: #ff0000;">'2010-04-18 00:00:00'</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">BETWEEN</span> <span style="color: #ff0000;">'2010-04-02 00:00:00'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #ff0000;">'2010-04-18 00:00:00'</span>
<span style="color: #808080; font-style: italic;">-- Ins?rer un message avec date</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> minichat<span style="color: #66cc66;">&#40;</span>pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Mateo'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Message !'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2010-04-02 16:32:22'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">Les fonctions de gestions des dates</h4> <br />
<br />
 <h4 class="formatter-title">NOW() : obtenir la date et l'heure actuelles</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- La date sera alors remplac?e par la date et l'heure actuelles au format AAAA-MM-JJ HH:MM:SS</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> minichat<span style="color: #66cc66;">&#40;</span>pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Mateo'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Message !'</span><span style="color: #66cc66;">,</span> NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="notice">Il existe aussi les fonctions CURDATE() et CURTIME() qui retournent respectivement uniquement la date (AAAA-MM-JJ) et l'heure (HH:MM:SS).</span><br />
<br />
 <h4 class="formatter-title">DAY(), MONTH(), YEAR() : extraire le jour, le mois ou l'ann?e</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> jour <span style="color: #993333; font-weight: bold;">FROM</span> minichat
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">HOUR(), MINUTE(), SECOND() : extraire les heures, minutes, secondes</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">HOUR</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> heure <span style="color: #993333; font-weight: bold;">FROM</span> minichat</pre></pre></div><br />
<br />
 <h4 class="formatter-title">DATE_FORMAT : formater une date</h4> <br />
<br />
DATE_FORMAT vous permet d'adapter directement la date au format que vous pr?f?rez.<br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- R?cup?ration des dates avec un champ nomm? date sous la forme 11/03/2010 15h47min49s.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> DATE_FORMAT<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'%d/%m/%Y %Hh%imin%ss'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">FROM</span> minichat
&nbsp;</pre></pre></div><br />
<br />
 <h4 class="formatter-title">DATE_ADD et DATE_SUB : ajouter ou soustraire des dates</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">-- Afficher une date d'expiration du message :  la date o? a ?t? post? le message + 15 jours</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> DATE_ADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">INTERVAL</span> <span style="color: #cc66cc;">15</span> <span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> date_expiration <span style="color: #993333; font-weight: bold;">FROM</span> minichat
<span style="color: #808080; font-style: italic;">-- Indiquer que les messages expirent dans deux mois </span>
<span style="color: #993333; font-weight: bold;">SELECT</span> pseudo<span style="color: #66cc66;">,</span> message<span style="color: #66cc66;">,</span> DATE_ADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">INTERVAL</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> date_expiration <span style="color: #993333; font-weight: bold;">FROM</span> minichat
&nbsp;</pre></pre></div><br />
<br />
<span class="warning">Le mot-cl? INTERVAL ne doit pas ?tre chang? ; en revanche, vous pouvez remplacer DAY par MONTH, YEAR, HOUR, MINUTE, SECOND, etc.</span><br />
<br />
<br />
<h3 class="formatter-title">Les jointures entre table</h3><br />
<br />
Soit deux tables : <strong>jeux_videos</strong>(contenant description et prix des jeux) et <strong>proprietaires</strong> (contenant le nom et adresse des propri?taires de jeux).<br />
Les jeux et leurs propri?taires ont une correspondance via le champ ID_proprietaire (de la table jeux_video) et le champ ID (de la table proprietaires)<br />
<br />
<strong>Les jointures internes :</strong> elles ne s?lectionnent que les donn?es qui ont une correspondance entre les deux tables ;<br />
<strong>Les jointures externes :</strong> elles s?lectionnent toutes les donn?es, m?me si certaines n'ont pas de correspondance dans l'autre table.<br />
<br />
 <h4 class="formatter-title">Les jointures internes</h4> <br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- Jointure interne avec WHERE</span>
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">--On demande clairement de r?cup?rer le nom du jeu et le pr?nom du propri?taire avec cette requ?te.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> jeux_video<span style="color: #66cc66;">.</span>nom<span style="color: #66cc66;">,</span> proprietaires<span style="color: #66cc66;">.</span>prenom
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires<span style="color: #66cc66;">,</span> jeux_video
<span style="color: #993333; font-weight: bold;">WHERE</span> jeux_video<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> proprietaires<span style="color: #66cc66;">.</span>ID
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- Utilisation des alias</span>
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- il est possible de donner des alias aux noms de champs et noms de tables</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom <span style="color: #993333; font-weight: bold;">AS</span> nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom <span style="color: #993333; font-weight: bold;">AS</span> prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires <span style="color: #993333; font-weight: bold;">AS</span> p<span style="color: #66cc66;">,</span> jeux_video <span style="color: #993333; font-weight: bold;">AS</span> j
<span style="color: #993333; font-weight: bold;">WHERE</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
<span style="color: #808080; font-style: italic;">--Le mot cl? AS est facultatif</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires p<span style="color: #66cc66;">,</span> jeux_video j
<span style="color: #993333; font-weight: bold;">WHERE</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- Jointure interne avec JOIN</span>
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">--On r?cup?re les donn?es depuis une table principale (proprietaires) et on fait une jointure interne avec une autre table (jeux_video)</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires p
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> jeux_video j
<span style="color: #993333; font-weight: bold;">ON</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- Introduire des filtres avec WHERE, ORDER BY, LIMIT</span>
<span style="color: #808080; font-style: italic;">--===================================</span>
<span style="color: #808080; font-style: italic;">-- R?cup?re le nom du jeu et le pr?nom du propri?taire dans les tables proprietaires et jeux_video, </span>
<span style="color: #808080; font-style: italic;">-- la liaison entre les tables se fait entre les champs ID_proprietaire et ID,</span>
<span style="color: #808080; font-style: italic;">-- prends uniquement les jeux qui tournent sur PC, </span>
<span style="color: #808080; font-style: italic;">--trie-les par prix d?croissants et ne prends que les 10 premiers. </span>
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires p
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> jeux_video j
<span style="color: #993333; font-weight: bold;">ON</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
<span style="color: #993333; font-weight: bold;">WHERE</span> j<span style="color: #66cc66;">.</span>console <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PC'</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> prix <span style="color: #993333; font-weight: bold;">DESC</span>
<span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">10</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"><br />
WHERE et JOIN permettent d'effectuer des jointures internes. Il est conseill? d'utiliser JOIN qui est beaucoup plus efficace.<br />
</span><br />
<br />
<br />
 <h4 class="formatter-title">Les jointures externes</h4> <br />
<br />
Les jointures externes permettent de r?cup?rer toutes les donn?es, m?me celles qui n'ont pas de correspondance.<br />
<br />
<strong><span style="color:blue;">LEFT JOIN : r?cup?rer toute la table de gauche</span></strong><br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires p
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> jeux_video j
<span style="color: #993333; font-weight: bold;">ON</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
&nbsp;</pre></pre></div><br />
<br />
<span class="success"><br />
proprietaires est appel?e la ? table de gauche ? et jeux_video la ? table de droite ?. Le LEFT JOIN demande ? r?cup?rer tout le contenu de la table de gauche, donc tous les propri?taires, m?me si ces derniers n'ont pas d'?quivalence dans la table jeux_video.<br />
</span><br />
<br />
<strong><span style="color:blue;">RIGHT JOIN : r?cup?rer toute la table de droite</span></strong><br />
<br />
<span class="formatter-code">Code SQL :</span><div class="code"><pre style="display:inline;"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> j<span style="color: #66cc66;">.</span>nom nom_jeu<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>prenom prenom_proprietaire
<span style="color: #993333; font-weight: bold;">FROM</span> proprietaires p
<span style="color: #993333; font-weight: bold;">RIGHT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> jeux_video j
<span style="color: #993333; font-weight: bold;">ON</span> j<span style="color: #66cc66;">.</span>ID_proprietaire <span style="color: #66cc66;">=</span> p<span style="color: #66cc66;">.</span>ID
&nbsp;</pre></pre></div><br />
<br />
<br />
[page]Les expressions r?guli?res[/page]<br />
<br />
Nous allons utiliser les expressions r?guli?res de type PCRE issues du langage Perl.<br />
<br />
Les fonctions qui nous int?ressent sont :<br />
preg_grep ; preg_split ; preg_quote ; preg_match ; preg_match_all ; preg_replace ;    preg_replace_callback.<br />
<br />
<h3 class="formatter-title">Recherches simples</h3><br />
<br />
<span class="warning">une regex (Expression r?guli?re) est toujours entour?e de caract?res sp?ciaux appel?s d?limiteurs.</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Recherche du mot guitare</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#guitare#"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'FAUX'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/*==========================================
//Recherche insensible ? la casse : utilisation de i
==========================================*/</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#guitare#i"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/*==========================================
//Le symbole ou : |
==========================================*/</span>
<span style="color: #008000; font-style: italic;">//Recherche du mot guitare ou piano</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#guitare|piano#"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span>
<span style="color: #008000; font-style: italic;">/*==========================================
//D?but (^) et fin($) de cha?ne
==========================================*/</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#^guitare#"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//si la chaine commence par guitare</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#guitare$#"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//si la chaine finit par guitare</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/preg_match"><span style="color: #0000FF; font-weight: bold;">preg_match</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">"#^guitare$#"</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">"J'aime jouer de la guitare."</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>  <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'VRAI'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">//La cha?ne doit contenir uniquement ? guitare ?</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<br />
<h3 class="formatter-title">Les classes de caract?res</h3><br />
    <br />
<strong>#gr[io]s#</strong>: Une des lettres ? l'int?rieur peut convenir. Notre regex reconna?t deux mots : ? gris ? et ? gros ?.<br />
<strong>#[aeiouy]$#</strong> : Notre regex se termine par une voyelle<br />
<strong>[a-z]</strong> : Toutes les lettres de l'alphabet en minuscule<br />
<strong>[a-e]</strong> : Les lettres de a ? e<br />
<strong>[0-9]</strong> : Les chiffres de 0 ? 9.<br />
[a-z0-9]. Cela signifie ? N'importe quelle lettre (minuscule) OU un chiffre ?.<br />
<br />
<span class="notice">Si vous ne voulez PAS des caract?res que vous ?num?rez dans votre classe, vous devrez placer le symbole ? ^ ? au d?but de la classe.</span><br />
<br />
<strong>#[^0-9]# </strong> : Je ne veux pas de chiffre<br />
<strong>#[^A-Z0-9]#</strong> : Ni majuscule, ni chiffre<br />
<strong>#^[^a-z]#</strong> : Ne commence pas par une minuscule<br />
<strong>#[^aeiouy]$#</strong>: Ne se termine pas par une voyelle<br />
<br />
<h3 class="formatter-title">Les quantificateurs</h3><br />
<br />
<strong><span style="color:blue;">Trois symboles ? retenir : </span></strong><br />
<br />
<strong>? (point d'interrogation) </strong>: La lettre est facultative. Elle peut y ?tre 0 ou 1 fois. Ainsi, #a?# reconna?t 0 ou 1 ? a ? ;<br />
<strong> + (signe plus) </strong>: la lettre est obligatoire. Elle peut appara?tre 1 ou plusieurs fois.  Ainsi, #a+# reconna?t ? a ?, ? aa ?, ? aaa ?, ? aaaa ?, etc. ;<br />
<strong> * (?toile)</strong> : la lettre est facultative. Elle peut appara?tre 0, 1 ou plusieurs fois.  Ainsi, #a*# reconna?t ? a ?, ? aa ?, ? aaa ?, ? aaaa ?, etc. Mais s'il n'y a pas de ? a ?, ?a fonctionne aussi !<br />
<br />
<h3 class="formatter-title">?tre plus pr?cis gr?ce aux accolades</h3><br />
<br />
{3} : la lettre (ou le groupe de lettres s'il est entre parenth?ses) doit ?tre r?p?t?e 3 fois exactement.<br />
#a{3}# fonctionne donc pour la cha?ne ? aaa ?.<br />
<br />
{3,5} : ici, on a plusieurs possibilit?s. On peut avoir la lettre de 3 ? 5 fois.<br />
#a{3,5}# fonctionne pour ? aaa ?, ? aaaa ?, ? aaaaa ?.<br />
<br />
{3,} : si vous mettez une virgule, mais pas de 2e nombre, ?a veut dire qu'il peut y en avoir jusqu'? l'infini. Ici, cela signifie ? 3 fois ou plus ?.<br />
#a{3,} fonctionne pour ? aaa ?, ? aaaa ?, ? aaaaa ?, ? aaaaaa ?, etc. <br />
<br />
<br />
<span class="success"><br />
Si vous faites attention, vous remarquez que :<br />
    ? revient ? ?crire {0,1} ;<br />
    + revient ? ?crire {1,} ;<br />
    * revient ? ?crire {0,}.<br />
</span><br />
<br />
<h3 class="formatter-title">Les caract?res sp?ciaux</h3><br />
<br />
# ! ^ $ ( ) [ ] { } ? + * . \ |<br />
<br />
Pour les ?chapper dans une expression recherch?e, Il va falloir l'?chapper. Cela signifie que vous devez placer en fait un antislash ? \ ? devant un caract?re sp?cial.<br />
<br />
<span class="warning">Et si vous voulez ajouter le tiret dans la liste des caract?res possibles ? Eh bien il suffit de le mettre soit au d?but de la classe, soit ? la fin. Par exemple : [a-z0-9-] permet de chercher une lettre, un chiffre ou un tiret.</span><br />
<br />
<h3 class="formatter-title">Capture et remplacement</h3><br />
<br />
Nous allons utiliser la fonction preg_replace pour capturer et remplacer une cha?ne. La cha?ne ? capturer est mise entre parenth?se.<br />
<br />
<span style="color:blue;">Exemple:</span><br />
#\[b\](.+)\[/b\]# : Chercher dans la cha?ne un [ b ], suivi d'un ou plusieurs caract?re(s) (le point permet de dire ? n'importe lesquels ?), suivi(s) d'un [ /b ]<br />
<br />
<span class="notice">A chaque fois qu'il y a une parenth?se, cela cr?e une variable appel?e $1 (pour la premi?re parenth?se), $2 pour la seconde, etc.<br />
On va ensuite se servir de ces variables pour modifier la cha?ne (faire un remplacement).</span><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">//Met en gras les mots plac?s entre [b][/b]</span>
<span style="color: #000080;">$texte</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #0000FF; font-weight: bold;">preg_replace</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'#\[b\](.+)\[/b\]#i'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'<strong>$1</strong>'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$texte</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<strong><span style="color:blue;">Les options</span></strong><br />
i : pour accepter les majuscules comme les minuscules ([B ] et [b ]) ;<br />
s : pour que le ? point ? fonctionne aussi pour les retours ? la ligne (pour que le texte puisse ?tre en gras sur plusieurs lignes) ;<br />
U : le U majuscule est une option que vous ne connaissez pas et qui signifie ? Ungreedy ? (? pas gourmand ?). Sachez que, grosso modo, ?a ne marcherait pas correctement s'il y avait plusieurs [b ] dans votre texte.? sans activer l'option Ungreedy, la regex aurait voulu mettre en gras tout ce qu'il y a entre le premier [b ] et le dernier [/b ].<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$texte</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #0000FF; font-weight: bold;">preg_replace</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'#\[b\](.+)\[/b\]#isU'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'<strong>$1</strong>'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$texte</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$texte</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #0000FF; font-weight: bold;">preg_replace</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'#\[i\](.+)\[/i\]#isU'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'<em>$1</em>'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$texte</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
[page]La programmation orient?e objet[/page]<br />
<br />
<h3 class="formatter-title">Cr?er une class</h3><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">class</span> Membre
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">private</span> <span style="color: #000080;">$pseudo</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">private</span> <span style="color: #000080;">$email</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">private</span> <span style="color: #000080;">$signature</span><span style="color: #8000FF;">;</span>
    <span style="color: #0000FF; font-weight: bold;">private</span> <span style="color: #000080;">$actif</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">//fonction constructeur</span>
<span style="color: #008000; font-style: italic;">//Le r?le d'une fonction constructeur est justement de construire l'objet,</span>
<span style="color: #008000; font-style: italic;">// c'est-?-dire de le pr?parer ? une premi?re utilisation.</span>
<span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> __construct<span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$idMembre</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// R?cup?rer en base de donn?es les infos du membre</span>
        <span style="color: #008000; font-style: italic;">// SELECT pseudo, email, signature, actif FROM membres WHERE id = ...</span>
&nbsp;
        <span style="color: #008000; font-style: italic;">// D?finir les variables avec les r?sultats de la base</span>
        <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">pseudo</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'pseudo'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
        <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">email</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$donnees</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'email'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
        <span style="color: #008000; font-style: italic;">// etc.</span>
    <span style="color: #8000FF;">&#125;</span> 
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> envoyerEMail<span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$titre</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$message</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <a href="http://www.php.net/mail"><span style="color: #0000FF; font-weight: bold;">mail</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">email</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$titre</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$message</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #8000FF;">&#125;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> bannir<span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">actif</span> <span style="color: #8000FF;">=</span> <span style="">false</span><span style="color: #8000FF;">;</span>
        <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">envoyerEMail</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Vous avez ?t? banni'</span><span style="color: #8000FF;">,</span> <span style="color: #808080;">'Ne revenez plus !'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #8000FF;">&#125;</span>
  <span style="color: #008000; font-style: italic;">//les getters et les setters</span>
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> getPseudo<span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #0000FF; font-weight: bold;">return</span> <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">pseudo</span><span style="color: #8000FF;">;</span>
    <span style="color: #8000FF;">&#125;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> setPseudo<span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nouveauPseudo</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">!</span><a href="http://www.php.net/empty"><span style="color: #0000FF; font-weight: bold;">empty</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nouveauPseudo</span><span style="color: #8000FF;">&#41;</span> AND <a href="http://www.php.net/strlen"><span style="color: #0000FF; font-weight: bold;">strlen</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$nouveauPseudo</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;"><</span> <span style="color: #FF8000;">15</span><span style="color: #8000FF;">&#41;</span>
        <span style="color: #8000FF;">&#123;</span>
            <span style="color: #000080;">$this</span><span style="color: #8000FF;">-></span><span style="color: #000000;">pseudo</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$nouveauPseudo</span><span style="color: #8000FF;">;</span>
        <span style="color: #8000FF;">&#125;</span>
    <span style="color: #8000FF;">&#125;</span>  
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<h3 class="formatter-title">Cr?er un objet</h3><br />
<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Membre.class.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #000080;">$membre</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> Membre<span style="color: #8000FF;">&#40;</span><span style="color: #FF8000;">32</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$membre</span><span style="color: #8000FF;">-></span><span style="color: #000000;">setPseudo</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$membre</span><span style="color: #8000FF;">-></span><span style="color: #000000;">getPseudo</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">', je vais te bannir !'</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$membre</span><span style="color: #8000FF;">-></span><span style="color: #000000;">bannir</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> 
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="notice"><br />
Pour d?truire un objet, ou toute autre variable, on peut le faire ? la main avec la fonction unset() :<br />
<?php<br />
unset($membre);<br />
?><br />
</span><br />
<br />
<h3 class="formatter-title">H?ritage</h3><br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'Membre.class.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">class</span> Admin <span style="color: #0000FF; font-weight: bold;">extends</span> Membre
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">private</span> <span style="color: #000080;">$couleur</span><span style="color: #8000FF;">;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> setCouleur<span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// ...</span>
    <span style="color: #8000FF;">&#125;</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">public</span> <span style="color: #0000FF; font-weight: bold;">function</span> getCouleur<span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span>
    <span style="color: #8000FF;">&#123;</span>
        <span style="color: #008000; font-style: italic;">// ...</span>
    <span style="color: #8000FF;">&#125;</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="warning"> C'est cela l'h?ritage : nous avons maintenant une classe Admin qui poss?de toutes les variables et fonctions de Membre, et nous allons pouvoir en d?finir de nouvelles qui seront propres aux admins.</span><br />
<br />
<h3 class="formatter-title">Les droits d'acc?s</h3><br />
<br />
Il y a trois droits d'acc?s ? conna?tre :<br />
<br />
    public : tout le monde peut acc?der ? l'?l?ment ;<br />
    private : personne (? part la classe elle-m?me) n'a le droit d'acc?der ? l'?l?ment ;<br />
    protected : identique ? private, sauf qu'un ?l?ment ayant ce droit d'acc?s dans une classe m?re sera accessible aussi dans les classes filles.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #000080;">$membre</span> <span style="color: #8000FF;">=</span> <span style="color: #0000FF; font-weight: bold;">new</span> Membre<span style="color: #8000FF;">&#40;</span><span style="color: #FF8000;">4</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$membre</span><span style="color: #8000FF;">-></span><span style="color: #000000;">setPseudo</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// OK car setPseudo est public</span>
<span style="color: #000080;">$membre</span><span style="color: #8000FF;">-></span><span style="color: #000000;">pseudo</span> <span style="color: #8000FF;">=</span> <span style="color: #808080;">'M@teo21'</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// Interdit car $pseudo est private</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;</pre></pre></div><br />
<br />
<span class="notice">Toutes les variables d'une classe doivent toujours ?tre priv?es ou prot?g?es.</span><br />
<br />
<br />
[page]Organiser son code selon l'architecture MVC[/page]<br />
<br />
<span style="color:blue;"><strong>Mod?le - Vue - Contr?leur</strong></span><br />
<br />
<strong> Mod?le</strong> : cette partie g?re les donn?es de votre site. On y trouve donc les requ?tes SQL.<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">function</span> get_billets<span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$offset</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$limit</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">global</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$offset</span> <span style="color: #8000FF;">=</span> <span style="color: #8000FF;">&#40;</span>int<span style="color: #8000FF;">&#41;</span> <span style="color: #000080;">$offset</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$limit</span> <span style="color: #8000FF;">=</span> <span style="color: #8000FF;">&#40;</span>int<span style="color: #8000FF;">&#41;</span> <span style="color: #000080;">$limit</span><span style="color: #8000FF;">;</span>
&nbsp;
    <span style="color: #000080;">$req</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$bdd</span><span style="color: #8000FF;">-></span><span style="color: #000000;">prepare</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'SELECT id, titre, contenu, DATE_FORMAT(date_creation, \'%d/%m/%Y ? %Hh%imin%ss\') AS date_creation_fr FROM billets ORDER BY date_creation DESC LIMIT :offset, :limit'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">bindParam</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">':offset'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$offset</span><span style="color: #8000FF;">,</span> PDO<span style="color: #8000FF;">::</span><span style="color: #000000;">PARAM_INT</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">bindParam</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">':limit'</span><span style="color: #8000FF;">,</span> <span style="color: #000080;">$limit</span><span style="color: #8000FF;">,</span> PDO<span style="color: #8000FF;">::</span><span style="color: #000000;">PARAM_INT</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">execute</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$billets</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$req</span><span style="color: #8000FF;">-></span><span style="color: #000000;">fetchAll</span><span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">return</span> <span style="color: #000080;">$billets</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;</pre></pre></div><br />
<br />
<strong>Contr?leur</strong> : cette partie g?re la logique du code qui prend des d?cisions. Le contr?leur va demander au mod?le les donn?es, les analyser, prendre des d?cisions et renvoyer le texte ? afficher ? la vue. Le contr?leur contient exclusivement du PHP.<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #008000; font-style: italic;">// On demande les 5 derniers billets (mod?le)</span>
<span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'modele/blog/get_billets.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #000080;">$billets</span> <span style="color: #8000FF;">=</span> get_billets<span style="color: #8000FF;">&#40;</span><span style="color: #FF8000;">0</span><span style="color: #8000FF;">,</span> <span style="color: #FF8000;">5</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// On effectue du traitement sur les donn?es (contr?leur)</span>
<span style="color: #008000; font-style: italic;">// Ici, on doit surtout s?curiser l'affichage</span>
<span style="color: #0000FF; font-weight: bold;">foreach</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$billets</span> <span style="color: #0000FF; font-weight: bold;">as</span> <span style="color: #000080;">$cle</span> <span style="color: #8000FF;">=></span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #000080;">$billets</span><span style="color: #8000FF;">&#91;</span><span style="color: #000080;">$cle</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'titre'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/htmlspecialchars"><span style="color: #0000FF; font-weight: bold;">htmlspecialchars</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'titre'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$billets</span><span style="color: #8000FF;">&#91;</span><span style="color: #000080;">$cle</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'contenu'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/nl2br"><span style="color: #0000FF; font-weight: bold;">nl2br</span></a><span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/htmlspecialchars"><span style="color: #0000FF; font-weight: bold;">htmlspecialchars</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'contenu'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">// On affiche la page (vue)</span>
<span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'vue/blog/index.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;</pre></pre></div><br />
<br />
<strong>Vue </strong>: cette partie se concentre sur l'affichage. On y trouve essentiellement du code HTML mais aussi quelques boucles et conditions PHP tr?s simples.<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
    <head>
        <title>Mon blog</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="vue/blog/style.css" rel="stylesheet" type="text/css" />
    </head>
&nbsp;
    <body>
        <h1>Mon super blog !</h1>
        <p>Derniers billets du blog :</p>
&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">foreach</span><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$billets</span> <span style="color: #0000FF; font-weight: bold;">as</span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
<div class="news">
    <h3>
        <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'titre'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span>
        <em>le <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'date_creation_fr'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span></em>
    </h3>
&nbsp;
    <p>
    <span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'contenu'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span>
    <br />
    <em><a href="commentaires.php?billet=<span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #000080;">$billet</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'id'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span>">Commentaires</a></em>
    </p>
</div>
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
</body>
</html>
&nbsp;</pre></pre></div><br />
<br />
<span style="color:blue;"><strong>Le contr?leur global du blog</strong></span><br />
Son r?le sera essentiellement de traiter les param?tres $_GET et d'appeler le contr?leur correspondant en fonction de la page demand?e. On peut aussi profiter de ce point ? central ? pour cr?er la connexion ? la base de donn?es.<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'modele/connexion_sql.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><span style="color: #8000FF;">!</span><a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'section'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span> OR <span style="color: #000080;">$_GET</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'section'</span><span style="color: #8000FF;">&#93;</span> <span style="color: #8000FF;">==</span> <span style="color: #808080;">'index'</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">include_once</span><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'controleur/blog/index.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;</pre></pre></div><br />
<br />
<span class="success"><br />
Vous noterez que ce fichier PHP ne contient pas la balise de fermeture ?>. Celle-ci n'est en effet pas obligatoire, comme je vous l'ai dit plus t?t dans le cours. Je vous recommande de ne pas l'?crire surtout dans le mod?le et le contr?leur d'une architecture MVC. Cela permet d'?viter de f?cheux probl?mes li?s ? l'envoi de HTML avant l'utilisation de fonctions comme setCookie qui n?cessitent d'?tre appel?es avant tout code HTML.<br />
</span><br />
<br />
[page]Prot?gez un dossier avec .htacces[/page]<br />
<br />
<h3 class="formatter-title">Cr?er le fichier .htacces</h3><br />
<br />
Cr?er un fichier avec le nom ".htacces" dans le dossier et mettez-y le contenu suivant :<br />
<br />
<span class="formatter-code">Code :</span><div class="code"><code><span style="color: #000000">
<br />AuthName&nbsp;"Entrez&nbsp;vos&nbsp;identifiants"<br />AuthType&nbsp;Basic<br />AuthUserFile&nbsp;"/home/site/www/admin/.htpasswd"<br />Require&nbsp;valid-user<br /></span>
</code></div> <br />
<br />
<strong>AuthName </strong>: c'est le texte qui invitera l'utilisateur ? inscrire son login et son mot de passe. Vous pouvez personnaliser ce texte comme bon vous semble ;<br />
<strong>AuthUserFile</strong> : c'est le chemin absolu vers le fichier .htpasswd (que vous mettrez dans le m?me r?pertoire que le .htaccess).<br />
<br />
<span class="warning">Pour trouver le chemin absolu d'un fichier, utilisez la fonction PHP realpath<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;"><span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <a href="http://www.php.net/realpath"><span style="color: #0000FF; font-weight: bold;">realpath</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'fichier.php'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span></pre></pre></div></span><br />
<br />
<h3 class="formatter-title">Cr?er le fichier .htpasswd</h3><br />
<br />
Le .htpasswd va contenir la liste des personnes autoris?es ? acc?der aux pages du dossier. On y inscrit une personne par ligne, sous cette forme :<br />
<br />
<span class="formatter-code">Code :</span><div class="code"><code><span style="color: #000000">
login:mot_de_passe_crypt?</span>
</code></div><br />
<br />
<span class="warning">Pour crypter un mot de passe :<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;"><span style="color: #FF0000; font-weight: normal;"><?php</span> <span style="color: #0000FF; font-weight: bold;">echo</span> <a href="http://www.php.net/crypt"><span style="color: #0000FF; font-weight: bold;">crypt</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #808080;">'kangourou'</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #FF0000; font-weight: normal;">?></span></pre></pre></div></span><br />
<br />
Ci-dessous une page php permettant de crypter automatiquement les mots de passe et d'afficher la ligne ? copier dans .htpasswd :<br />
<br />
<span class="formatter-code">Code PHP :</span><div class="code"><pre style="display:inline;"><pre class="php" style="font-family:monospace;"><span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #8000FF;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'login'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span> AND <a href="http://www.php.net/isset"><span style="color: #0000FF; font-weight: bold;">isset</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'pass'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">&#41;</span>
<span style="color: #8000FF;">&#123;</span>
    <span style="color: #000080;">$login</span> <span style="color: #8000FF;">=</span> <span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'login'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">;</span>
    <span style="color: #000080;">$pass_crypte</span> <span style="color: #8000FF;">=</span> <a href="http://www.php.net/crypt"><span style="color: #0000FF; font-weight: bold;">crypt</span></a><span style="color: #8000FF;">&#40;</span><span style="color: #000080;">$_POST</span><span style="color: #8000FF;">&#91;</span><span style="color: #808080;">'pass'</span><span style="color: #8000FF;">&#93;</span><span style="color: #8000FF;">&#41;</span><span style="color: #8000FF;">;</span> <span style="color: #008000; font-style: italic;">// On crypte le mot de passe</span>
&nbsp;
    <span style="color: #0000FF; font-weight: bold;">echo</span> <span style="color: #808080;">'<p>Ligne ? copier dans le .htpasswd :<br />'</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$login</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">':'</span> <span style="color: #8000FF;">.</span> <span style="color: #000080;">$pass_crypte</span> <span style="color: #8000FF;">.</span> <span style="color: #808080;">'</p>'</span><span style="color: #8000FF;">;</span>
<span style="color: #8000FF;">&#125;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #008000; font-style: italic;">// On n'a pas encore rempli le formulaire</span>
<span style="color: #8000FF;">&#123;</span>
<span style="color: #FF0000; font-weight: normal;">?></span>
&nbsp;
<p>Entrez votre login et votre mot de passe pour le crypter.</p>
&nbsp;
<form method="post">
    <p>
        Login : <input type="text" name="login"><br />
        Mot de passe : <input type="text" name="pass"><br /><br />
&nbsp;
        <input type="submit" value="Crypter !">
    </p>
</form>
&nbsp;
<span style="color: #FF0000; font-weight: normal;"><?php</span>
<span style="color: #8000FF;">&#125;</span>
<span style="color: #FF0000; font-weight: normal;">?></span></pre></pre></div>]]></description>
			<pubDate>Sun, 16 Jun 2013 11:10:00 +0200</pubDate>
			
		</item>
		
	</channel>
</rss>
