Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 ·...
Transcript of Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 ·...
![Page 1: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/1.jpg)
IntrodutionàPHP,MySQLetAJAX
![Page 2: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/2.jpg)
Contenuducours
• IntroductionàPHP– Syntaxe– Variables– Boucles– …
• PHPavancé– Sessions– Manipulationdefichiers– Cookies– …
• PHP+MySQL• AJAX
![Page 3: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/3.jpg)
Qu'est‐cequePHP?
• PHPsignifiePHP:HypertextPreprocessor• PHPestunlangagedescriptcôtéserveur,commel'ASP
• LesscriptsPHPsontexécutéssurleserveur• PHPsupportedenombreusesbasesdedonnées(MySQL,Informix,Oracle,Sybase,Solid,PostgreSQL,ODBCgénériques,etc)
• PHPestunlogicielopensource• PHPestgratuitàtéléchargeretàutiliser
![Page 4: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/4.jpg)
Qu'est‐cequequ’unfichierPHP?
• LesfichiersPHPpeuventcontenirdutexte,desbalisesHTMLetdesscripts
• LesfichiersPHPsontretournésaunavigateurHTML
• LesfichiersPHPontuneextensiondefichier".Php",".Php3",ou".Phtml"
![Page 5: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/5.jpg)
Qu'est‐cequeMySQL?
• MySQLestunserveurdebasededonnées• MySQLestidéalpourlespetitesetlesgrandesapplications
• MySQLsupportelanormeSQL• MySQLcompilesuruncertainnombredeplates‐formes
• MySQLestgratuitàtéléchargeretàutiliser• PHPassociéàMySQLsontmulti‐plateforme(vouspouvezdéveloppersousWindowsetdéployersousUnix)
![Page 6: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/6.jpg)
PourquoiPHP?
• PHPfonctionnesurdifférentesplates‐formes(Windows,Linux,Unix,etc)
• PHPestcompatibleavecpresquetouslesserveursutilisésaujourd'hui(Apache,IIS,etc)
• PHPestgratuitàtéléchargersurwww.php.net• PHPestfacileàapprendreetfonctionnedemanièreefficacecôtéserveur
![Page 7: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/7.jpg)
SyntaxedebaseenPHP
• UnblocdescriptPHPcommencepar<?Phpetsetermineavec?>• UnblocdescriptPHPpeutêtreplacén'importeoùdansle
document.
• Certainsserveurssupportentlasyntaxe<?et?>
• Pourunecompatibilitémaximale,ilestrecommandéd'utiliserlaformestandard(<?Php)plutôtquelaformeabrégée.
• UnfichierPHPcontientnormalementdesbalisesHTMLetquelquesscriptsPHP
![Page 8: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/8.jpg)
Exemple
<html><body><?phpecho"HelloWorld";echo"Bonjourtoutlemonde";?></body></html>• ChaquelignedecodeenPHPdoitseterminerparunpoint‐virgule
• Lavirguleestunséparateuretestutiliséepourdistingueruneséried'instructionsd'uneautre
![Page 9: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/9.jpg)
CommentairesenPHP
• EnPHP,onutilise//pourfaireuneseulelignedecommentaireou/*et*/pourunbloc
<html><body><?Php//Ceciestuncommentaire/*Icic’estUnbloc*/?></body></html>
![Page 10: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/10.jpg)
LesvariablesenPHP
• Lesvariablessontutiliséespourstockerdesvaleurs,commeleschaînesdetexte,deschiffresoudestableaux
• Quandunevariableestdéfinie,ellepeutêtreutiliséplusieursfoisdansvotrescript
• TouteslesvariablesenPHPdébutentparlesymbole$
<?php$txt="Bonjourtoutlemonde!";$nombre=16;?>
![Page 11: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/11.jpg)
VariablesPHPnontypées
• EnPHP,unevariablePHPn'apasbesoind'êtredéclaréeavantd'êtrefixé
• Dansl'exempleprécédent,onapasbesoindedéfinirletypededonnéesquevacontenirlavariable
• PHPconvertitautomatiquementlavariableaubontypededonnées,selonlafaçondontilssontfixés(commeenjavascript)
![Page 12: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/12.jpg)
Variables:règlesdenommage
• Unnomdevariabledoitcommencerparunelettreouuncaractèredesoulignement"_»
• Unnomdevariablenepeutcontenirquedescaractèresalphanumériquesetdestraitsdesoulignement(az,AZ,0‐9et_)
• Unnomdevariablenedevraitpascontenird'espaces
• Siunnomdevariableestenplusieursmots,ildevraêtreséparéparlecaractèredesoulignement($my_string),ouavecmajuscule($maChaine)
![Page 13: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/13.jpg)
Chaînesdecaractères
• Exemple:
• Concaténation(.):
• Longueurd’unechaîne(strlen()):
• Positiond’unesous‐chaîne(strpos()):
<?php$txt="HelloWorld";echo$txt;?>
<?php$txt1="HelloWorld";$txt2="1234";echo$txt1."".$txt2;?>
<?phpechostrlen("Helloworld!");?>
<?phpechostrpos("Helloworld!","world");?>
Plusdefonctionssurhttp://w3schools.com/php/php_ref_string.asp
![Page 14: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/14.jpg)
OpérateursenPHP
• Opérateursarithmétiques
![Page 15: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/15.jpg)
OpérateursenPHP
• Opérateursd’affectation
![Page 16: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/16.jpg)
OpérateursenPHP
• Opérateursdecomparaison
![Page 17: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/17.jpg)
OpérateursenPHP
• Opérateurslogiques
![Page 18: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/18.jpg)
If…elseenPHP
• Syntaxe
• Exemples:
if(condition)codetobeexecutedifconditionistrue;elsecodetobeexecutedifconditionisfalse;
<html><body><?php$d=date("D");if($d=="Fri")echo"Haveaniceweekend!";elseecho"Haveaniceday!";?></body></html>
<html><body><?php$d=date("D");if($d=="Fri"){echo"Hello!<br/>";echo"Haveaniceweekend!";echo"SeeyouonMonday!";}?></body></html>
![Page 19: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/19.jpg)
elseifenPHP
• Syntaxe:
• Exemple:
if(condition)codetobeexecutedifconditionistrue;elseif(condition)codetobeexecutedifconditionistrue;elsecodetobeexecutedifconditionisfalse;
<html><body><?php$d=date("D");if($d=="Fri")echo"Haveaniceweekend!";elseif($d=="Sun")echo"HaveaniceSunday!";elseecho"Haveaniceday!";?></body></html>
![Page 20: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/20.jpg)
switchenPHP
• Syntaxe:
• Exemple:
switch(expression){caselabel1:codetobeexecutedifexpression=label1;break;caselabel2:codetobeexecutedifexpression=label2;break;default:codetobeexecutedifexpressionisdifferentfrombothlabel1andlabel2;}
<?phpswitch($x){case1:echo"Number1";break;case2:echo"Number2";break;default:echo"Nonumberbetween1and2";}?>
![Page 21: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/21.jpg)
TableauxenPHP
• Ilexistetroisdifférentstypesdetableaux:– UntableauavecunecléIDnumérique
– UntableauoùchaqueIDcléestassociéeàunevaleur
– Untableaucontenantunouplusieurstableaux
![Page 22: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/22.jpg)
Tableauxnumériques
• Ilexistedifférentesfaçonsdecréeruntableaunumérique:
$names=array("Peter","Quagmire","Joe");
<?php
$names[0]="Peter";$names[1]="Quagmire";$names[2]="Joe";
echo$names[1]."and".$names[2]."are".$name[0]."'sneighbors";?>
![Page 23: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/23.jpg)
Tableauxassociatifs
• Avecdestableauxassociatifs,nouspouvonsutiliserlesvaleursdeclésetleurassignerdesvaleurs:
$ages=array("Peter"=>32,"Quagmire"=>30,"Joe"=>34);
<?php
$ages['Peter']="32";$ages['Quagmire']="30";$ages['Joe']="34";
echo"Peteris".$ages['Peter']."yearsold.";?>
![Page 24: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/24.jpg)
Tableauxmultidimensionnels
• Exemple:
• Equivalentà:
$families=array("Griffin"=>array("Peter","Lois","Megan"),"Quagmire"=>array("Glenn"),"Brown"=>array("Cleveland","Loretta","Junior"));
$families=array([Griffin]=>Array([0]=>Peter,[1]=>Lois,[2]=>Megan)[Quagmire]=>Array([0]=>Glenn)[Brown]=>Array([0]=>Cleveland,[1]=>Loretta,[2]=>Junior))
![Page 25: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/25.jpg)
BouclesPHP
• While:
• Do…while:
<?php$i=1;while($i<=5){echo"Thenumberis".$i."<br/>";$i++;}?>
<?php$i=0;do{$i++;echo"Thenumberis".$i."<br/>";}while($i<5);?>
![Page 26: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/26.jpg)
BouclesPHP
• For:
• Foreach:
<?phpfor($i=1;$i<=5;$i++){echo"HelloWorld!<br/>";}?>
<?php$arr=array("one","two","three");
foreach($arras$value){echo"Value:".$value."<br/>";}?>
![Page 27: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/27.jpg)
FonctionsenPHP
• Unefonctionestunblocdecodequipeutêtreexécutéàchaquefoisnousenavonsbesoin
• CréationdefonctionsenPHP:
– Touteslesfonctionscommencentparlemot’function()’– Lenomdesfonctionsdoitêtreexplicite– Lesnomsdefonctionscommencentparunelettreouunderscore
– ()pourdéfinirlesparamètres– {}pourlecorpsdelafonction
![Page 28: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/28.jpg)
Exemple
<?phpfunctionwriteMyName(){echo"KaiJimRefsnes";}
echo"Helloworld!<br/>";echo"Mynameis";writeMyName();echo".<br/>That'sright,";writeMyName();echo"ismyname.";?>
![Page 29: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/29.jpg)
Paramètresdefonctions
<?phpfunctionwriteMyName($fname,$punctuation){echo$fname."Refsnes".$punctuation."<br/>";}
echo"Mynameis";writeMyName("KaiJim",".");
echo"Mynameis";writeMyName("Hege","!");
echo"Mynameis";writeMyName("Ståle","...");?>
![Page 30: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/30.jpg)
Valeurderetourd’unefonction
<?phpfunctionadd($x,$y){$total=$x+$y;return$total;}
echo"1+16=".add(1,16);?>
![Page 31: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/31.jpg)
Formulairesetchampsd’entrée
• Exemple:
• Lefichierwelcome.php:
<html><body><formaction="welcome.php"method="post">Name:<inputtype="text"name="name"/>Age:<inputtype="text"name="age"/><inputtype="submit"/></form></body></html>
<html><body>
Welcome<?phpecho$_POST["name"];?>.<br/>Youare<?phpecho$_POST["age"];?>yearsold.
</body></html>
![Page 32: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/32.jpg)
$_GET,$_POST,$_REQUEST
• Suivantlamanièred’envoyerlesdonnéessurleserveur,onutiliserasoit$_GETsoit$_POSTafinderécupérerleschampsetvaleursdesformulaires
• $_REQUESTcontientàlafois$_GET,$_POSTet$_COOKIE
![Page 33: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/33.jpg)
PHPavancé:quelquesfonctions
![Page 34: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/34.jpg)
Date()enPHP
• Syntaxe:date(format,timestamp)
– Format:spécifieleformatd’horodatage– Timestamp:tempsécoulédepuisle01/01/70(facultatif)
• Exemple:
• mktime(heure,minute,seconde,jour,mois,année,is_dst):pourdéfiniruntimestamp
<?phpechodate("Y/m/d");echo"<br/>";echodate("Y.m.d");echo"<br/>";echodate("Y‐m‐d");?>
![Page 35: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/35.jpg)
IncluredesfichiersenPHP
• Syntaxe:include(url)• Exemple:
<html><body>
<?phpinclude("header.php");?>
<h1>Welcometomyhomepage</h1>
<p>Sometext</p>
</body></html>
![Page 36: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/36.jpg)
IncluredesfichiersenPHP
• Permetfacilementdefaireunsqueletted’unsiteweb
• Exemple:<html><body><?php
include(‘header.php’);include(‘menu.php’);include(‘body.php’);include(‘footer.php’);
?></body></html>
![Page 37: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/37.jpg)
IncluredesfichiersenPHP
• Syntaxe:require(url)• Lorsd’uneerreur,lafonctioninclude()permetdecontinuerlechargementdelapage
• require()arrêtecomplètementceluici
• Exemple: <html><body><?phpinclude("wrongFile.php");echo"HelloWorld!";?></body></html>
![Page 38: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/38.jpg)
IncluredesfichiersenPHP
• Sortieavecinclude:
• Etavecrequire:
Warning:include(wrongFile.php)[function.include]:failedtoopenstream:NosuchfileordirectoryinC:\home\website\test.phponline5
Warning:include()[function.include]:Failedopening'wrongFile.php'forinclusion(include_path='.;C:\php5\pear')inC:\home\website\test.phponline5
HelloWorld!
Warning:require(wrongFile.php)[function.require]:failedtoopenstream:NosuchfileordirectoryinC:\home\website\test.phponline5
Fatalerror:require()[function.require]:Failedopeningrequired'wrongFile.php'(include_path='.;C:\php5\pear')inC:\home\website\test.phponline5
![Page 39: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/39.jpg)
Ouverture/fermeturedefichiersenPHP
• Syntaxeopen(file,mode)– File:urldufichier– Modeparmir,r+,w,w+,a,a+,x,x+
• Exemple:
• Fermeture:fclose()
<?php$file=fopen("welcome.txt","r")orexit("Unabletoopenfile!");?>
<?php$file=fopen("test.txt","r");//somecodetobeexecutedfclose($file);?>
![Page 40: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/40.jpg)
FindefichieretlectureenPHP
• Testerlafind’unfichier:feof($file)
• Lectureligneparligne:fgets($file)• Lecturecaractèreparcaractère:fgetc($file)• Exemple:
if(feof($file))echo"Endoffile";
<?php$file=fopen("welcome.txt","r")orexit("Unabletoopenfile!");while(!feof($file)){echofgetc($file);}fclose($file);?>
![Page 41: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/41.jpg)
UploaddefichiersenPHP
• Formulaired’upload:
• Scriptd’upload(serveur):
<html><body><formaction="upload_file.php"method="post"enctype="multipart/form‐data"><labelfor="file">Filename:</label><inputtype="file"name="file"id="file"/><br/><inputtype="submit"name="submit"value="Submit"/></form></body></html>
<?phpif($_FILES["file"]["error"]>0){echo"Error:".$_FILES["file"]["error"]."<br/>";}else{echo"Upload:".$_FILES["file"]["name"]."<br/>";echo"Type:".$_FILES["file"]["type"]."<br/>";echo"Size:".($_FILES["file"]["size"]/1024)."Kb<br/>";echo"Storedin:".$_FILES["file"]["tmp_name"];}?>
![Page 42: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/42.jpg)
UploaddefichiersenPHP
• Onrécupèreensuitelefichiersurleserveurgrâceàlavariable$_FILES(tableauàdeuxdimensions)– *$_FILES["file"]["name"]:nomdufichier– *$_FILES["file"]["type"]:typedufichier– *$_FILES["file"]["size"]:tailleenoctetsdufichier– *$_FILES["file"]["tmp_name"]:nomtemporairedufichiersurleserveur
– *$_FILES["file"]["error"]:coded’erreur
![Page 43: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/43.jpg)
UploaddefichiersenPHP
• Sauvegardedufichier: <?phpif((($_FILES["file"]["type"]=="image/gif")||($_FILES["file"]["type"]=="image/jpeg")||($_FILES["file"]["type"]=="image/pjpeg"))&&($_FILES["file"]["size"]<20000)){if($_FILES["file"]["error"]>0){echo"ReturnCode:".$_FILES["file"]["error"]."<br/>»;}else{echo"Upload:".$_FILES["file"]["name"]."<br/>";echo"Type:".$_FILES["file"]["type"]."<br/>";echo"Size:".($_FILES["file"]["size"]/1024)."Kb<br/>";echo"Tempfile:".$_FILES["file"]["tmp_name"]."<br/>";
if(file_exists("upload/".$_FILES["file"]["name"])){echo$_FILES["file"]["name"]."alreadyexists.»;}else{move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);echo"Storedin:"."upload/".$_FILES["file"]["name"];}}}else{echo"Invalidfile";}?>
![Page 44: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/44.jpg)
CookiesenPHP
• Seulmécanismeutilisableafindestockerdesinformationssurleclient
• Créationdecookies:setcookie(name,value,expire,path,domain);
• Exemple:<?phpsetcookie("user","AlexPorter",time()+3600);?>
![Page 45: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/45.jpg)
CookiesenPHP
• Récupérationd’uncookie:$_COOKIE• Exemple:
• Lafonctionisset()permetdevérifierl’existenceducookie
<?phpif(isset($_COOKIE["user"]))echo"Welcome".$_COOKIE["user"]."!<br/>";elseecho"Welcomeguest!<br/>";?>
![Page 46: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/46.jpg)
CookiesenPHP
• Suppressiond’uncookie:luimettreunedated’expirationantérieureàladateactuelle
• Exemple:<?php//settheexpirationdatetoonehouragosetcookie("user","",time()‐3600);?>
![Page 47: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/47.jpg)
SessionsenPHP
• Commentdonnerlapossibilitéàl’utilisateurdevisiterunsitesanspourautantperdredesinformationsqu’ilvoudraitstocker:mécanismedesessions– Permetdeconserverdesvaleursd’unformulairesanspasserparlescookies
– Gestiond’unpanierd’articlesdanslecadred’unsitee‐commerce
• Lesvariablesetobjetsdesessionssontstockéscôtéserveur
![Page 48: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/48.jpg)
SessionsenPHP
• Commencerunesession:session_start()• Exemple:
• Àinsérerobligatoirementavantletaghtml
<?phpsession_start();?><html><body></body></html>
![Page 49: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/49.jpg)
SessionsenPHP
• Stockerdesobjetsdanslasession:$_SESSION• Exemple:
<?phpsession_start();//storesessiondata$_SESSION['views']=1;?><html><body><?php//retrievesessiondataecho"Pageviews=".$_SESSION['views'];?></body></html>
![Page 50: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/50.jpg)
SessionsenPHP
• Testersiunevariableexiste:isset()• Exemple(compteurdepages):
• Lesvariablesstockéesdanslessessionssontvisiblesdanstouteslespagesdumêmesite
<?phpsession_start();if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo"Views=".$_SESSION['views'];?>
![Page 51: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/51.jpg)
SessionsenPHP
• Poursupprimerunevariablestockéedanslasession:unset()
• Exemple:
• Poursupprimerlasession:session_destroy()
• Exemple:
<?phpunset($_SESSION['views']);?>
<?phpsession_destroy();?>
![Page 52: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/52.jpg)
Envoyerdese‐mailsenPHP
• Lafonctionmail:mail(to,subject,message,headers,parameters)– To:l’adressedudestinataire– Subject:letitredumail– Message:lecorpsdumessage(\npourunenouvelleligne(limite70
caractères))– Headers:entêtesspécifiques(commeccetBcc)
– Parameters:(paramètresspécifiquespourleserveurdemail)
• Pourpouvoirl’utiliserilfautobligatoirementqu’unserveurdemailsoitinstallé(ex:sendmail)
![Page 53: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/53.jpg)
Envoyerdese‐mailsenPHP
• Exemple(simplemail): <?php
$to="[email protected]";$subject="Testmail";$message="Hello!Thisisasimpleemailmessage.";$from="[email protected]";$headers="From:$from";mail($to,$subject,$message,$headers);echo"MailSent.";
?>
![Page 54: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/54.jpg)
Envoyerdese‐mailsenPHP
• Récupérationàpartird’unformulaire:<html><body><?phpif(isset($_REQUEST['email']))//if"email"isfilledout,sendemail{//sendemail$email=$_REQUEST['email'];$subject=$_REQUEST['subject'];$message=$_REQUEST['message'];mail("[email protected]","Subject:$subject",$message,"From:$email");echo"Thankyouforusingourmailform";}else//if"email"isnotfilledout,displaytheform{echo"<formmethod='post'action='mailform.php'>Email:<inputname='email'type='text'/><br/>Subject:<inputname='subject'type='text'/><br/>Message:<br/><textareaname='message'rows='15'cols='40'></textarea><br/><inputtype='submit'/></form>";}?></body></html>
![Page 55: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/55.jpg)
Injectionse‐mail
• Dansl’exempleprécédent:ilestpossibledemodifierl’entêtedelarequêteafind’envoyerunmailàplusieurspersonnes
• Utilisationdelefonctionfilter_var($field,param)
– $field:variableàvérifier– Param:
• FILTER_SANITIZE_EMAIL:supprimelescaractèresillégaux
• FILTER_VALIDATE_EMAIL:valideounonlachaînecommeunmailvalide
• Plusd’informations:http://w3schools.com/php/php_filter.asp
![Page 56: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/56.jpg)
IntercepterdeserreursenPHP
• Exemple:onveutouvrirunfichierquin’existepas
• Utilisationdelafonctiondie():
• Possibilitédedéfinirsespropresinterceptionsetdéclencheurs
Warning:fopen(welcome.txt)[function.fopen]:failedtoopenstream:NosuchfileordirectoryinC:\webfolder\test.phponline2
<?phpif(!file_exists("welcome.txt")){die("Filenotfound");}else{$file=fopen("welcome.txt","r");}?>
![Page 57: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/57.jpg)
ExceptionsenPHP
• Exemple: <?php//createfunctionwithanexceptionfunctioncheckNum($number){if($number>1){thrownewException("Valuemustbe1orbelow");}returntrue;}//triggerexceptionina"try"blocktry{checkNum(2);//Iftheexceptionisthrown,thistextwillnotbeshownecho'Ifyouseethis,thenumberis1orbelow';}//catchexceptioncatch(Exception$e){echo'Message:'.$e‐>getMessage();}?>
![Page 58: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/58.jpg)
FiltresenPHP
• Mécanismepermettantdevérifierlavaliditédecertainschamps,informations
• Exemple(testd’unentier):
• D’autresfiltres:http://w3schools.com/php/php_ref_filter.asp
<?php$int=123;if(!filter_var($int,FILTER_VALIDATE_INT)){echo("Integerisnotvalid");}else{echo("Integerisvalid");}?>
![Page 59: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/59.jpg)
MySQL
• Introduction• Connexionàunebasededonnées• Créationdetables• Sélectiond’informationsdansuneBD(where,orderby)
• Insertion/maj/suppressiond’élémentsdansuneBD
![Page 60: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/60.jpg)
ConnexionàunebasededonnéesMySQL
• mysql_connect(server,user,password)– Server:localhost:3306(leportestmodifiable)
– User:lelogin– Password:unmotdepassesidéfinit
• Exemple:<?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}//somecode?>
![Page 61: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/61.jpg)
DéconnexiondeMySQL
• mysql_close($con)• Exemple:
<?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}
//somecode
mysql_close($con);?>
![Page 62: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/62.jpg)
CréationDB/tableaux
• mysql_query(‘requete’,$con)• Pourunebasededonnées:
<?php$con=mysql_connect("localhost","peter","abc123")ordie('Couldnotconnect:'.mysql_error());;if(mysql_query("CREATEDATABASEmy_db",$con)){echo"Databasecreated";}else{echo"Errorcreatingdatabase:".mysql_error();}mysql_close($con);?>
![Page 63: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/63.jpg)
CréationDB/tableaux
• Pouruntableau:
• LaDBdoitexisteretêtresélectionnéepourcréerlatable
mysql_select_db("my_db",$con);$sql="CREATETABLEPersons(FirstNamevarchar(15),LastNamevarchar(15),Ageint)";
//Executequerymysql_query($sql,$con);
![Page 64: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/64.jpg)
CréationDB/tableaux
• Pouruntableau(aveccléprimaire):
$sql="CREATETABLEPersons(personIDintNOTNULLAUTO_INCREMENT,PRIMARYKEY(personID),FirstNamevarchar(15),LastNamevarchar(15),Ageint)";
mysql_query($sql,$con);
![Page 65: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/65.jpg)
InsertMySQL
• Exemple: <?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}
mysql_select_db("my_db",$con);
mysql_query("INSERTINTOPersons(FirstName,LastName,Age)VALUES('Peter','Griffin','35')");
mysql_query("INSERTINTOPersons(FirstName,LastName,Age)VALUES('Glenn','Quagmire','33')");
mysql_close($con);?>
![Page 66: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/66.jpg)
InsertMySQL
• Avecunformulaire:
• Lefichierinsert.php:
<formaction="insert.php"method="post">Firstname:<inputtype="text"name="firstname"/>Lastname:<inputtype="text"name="lastname"/>Age:<inputtype="text"name="age"/><inputtype="submit"/></form>
<?php$con=mysql_connect("localhost","peter","abc123")ordie('Couldnotconnect:'.mysql_error());;mysql_select_db("my_db",$con);$sql="INSERTINTOPersons(FirstName,LastName,Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if(!mysql_query($sql,$con)){die('Error:'.mysql_error());}echo"1recordadded»;mysql_close($con)?>
![Page 67: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/67.jpg)
SelectMySQL
• Exemple1:<?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}mysql_select_db("my_db",$con);$result=mysql_query("SELECT*FROMPersons");while($row=mysql_fetch_array($result)){echo$row['FirstName']."".$row['LastName'];echo"<br/>";}mysql_close($con);?>
![Page 68: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/68.jpg)
SelectMySQL
• Exemple2: <?php$con=mysql_connect("localhost","peter","abc123");mysql_select_db("my_db",$con);$result=mysql_query("SELECT*FROMPersons");
echo"<tableborder='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
while($row=mysql_fetch_array($result)){echo"<tr>";echo"<td>".$row['FirstName']."</td>";echo"<td>".$row['LastName']."</td>";echo"</tr>";}echo"</table>";
mysql_close($con);?>
![Page 69: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/69.jpg)
WhereMySQL
• Exemple: <?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}mysql_select_db("my_db",$con);
$result=mysql_query("SELECT*FROMPersonsWHEREFirstName='Peter'");
while($row=mysql_fetch_array($result)){echo$row['FirstName']."".$row['LastName'];echo"<br/>";}?>
![Page 70: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/70.jpg)
OrderbyMySQL
• Exemple: <?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}mysql_select_db("my_db",$con);
$result=mysql_query("SELECT*FROMPersonsORDERBYage");
while($row=mysql_fetch_array($result)){echo$row['FirstName'];echo"".$row['LastName'];echo"".$row['Age'];echo"<br/>";}mysql_close($con);?>
![Page 71: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/71.jpg)
UpdateMySQL
• Exemple: <?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}mysql_select_db("my_db",$con);
mysql_query("UPDATEPersonsSETAge='36'WHEREFirstName='Peter'ANDLastName='Griffin'");
mysql_close($con);?>
![Page 72: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/72.jpg)
DeleteMySQL
• Exemple:
<?php$con=mysql_connect("localhost","peter","abc123");if(!$con){die('Couldnotconnect:'.mysql_error());}
mysql_select_db("my_db",$con);
mysql_query("DELETEFROMPersonsWHERELastName='Griffin'");
mysql_close($con);?>
![Page 73: Introdution à PHP, MySQL et AJAXrichard-ostrowski.eu/SILNTI/supports/IP6/php.pdf · 2014-01-27 · – Toutes les fonctions commencent par le mot ’function ()’ – Le nom des](https://reader034.fdocuments.us/reader034/viewer/2022050100/5f3fab171190ce54714d4cff/html5/thumbnails/73.jpg)
SQLréférences
• http://w3schools.com/sql/default.asp