epita advanced&web&development& and advanced&database
TRANSCRIPT
![Page 1: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/1.jpg)
EPITA Advanced WEB Development
and Advanced Database 2015 MI Fall Session
Teacher : Alexandra Champavert
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 1 -‐
![Page 2: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/2.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 2 -‐
![Page 3: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/3.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 3 -‐
![Page 4: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/4.jpg)
Before the Database management systems
• The first databases-‐like appeared in the 1950’s years in order to make easier the bank and insurance process
• During almost 20 years, the data were stored in flat files
• The main langage used to process these flat files was the COBOL
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 4 -‐
![Page 5: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/5.jpg)
Birth of the DBMS Concept
• SQL language crea>on • Strict spli`ng between the hardware hos>ng the data, and the data processing
• Data modeling • Normaliza>on crea>on • Boyce-‐Codd normal form • Database Management System Concept
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 5 -‐
![Page 6: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/6.jpg)
The database
• This is the place where the data is stored • The data storage implies the informa>on persistence
• The database isn’t alive • To be alive it needs a tool
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 6 -‐
![Page 7: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/7.jpg)
Database management
• A database is managed through a system • This system is named Database Management System, otherwise DBMS
• The management system gives the interface between the data and the user
• The management system is in charge for upda>ng the data
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 7 -‐
![Page 8: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/8.jpg)
ACID Proper>es
• Atomicity : A transac>on is atomic if it can be totally commited or rollbacked
• Coherency : A transac>on should not leave a database in an inconsistent state
• Isola>on : A transac>on can not view another running transac>on
• Durability : As the transac>on is commited, the modified data will not disapear
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 8 -‐
![Page 9: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/9.jpg)
Database Hardware
Infrastructure
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 9 -‐
![Page 10: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/10.jpg)
Infrastructure
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 10 -‐
Clients
Administra>on console
Applica>on servers
DB servers
Storage
Backup
Client network
Administra>on network
Data network
DB backup network (op>onal)
Backup network
Cluster network
DB Storage network (eventually backup too)
Applica>on Server Network Storage
![Page 11: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/11.jpg)
The storage, central element of the architecture
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 11 -‐
![Page 12: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/12.jpg)
The storage
• … is made of : – Enclosures, – RAID disks (0, 1, 5, 10, 50, security), – Copper lines (SCSI), – Fibre Channel, – Memory cache, – Dedicated Opera>ng System, – For some of them : backup baferies to prevent power cut
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 12 -‐
![Page 13: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/13.jpg)
The storage
• Three main storage types exist : – Internal storage (server) – NAS : Network Afached Storage – SAN : Storage Area Network
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 13 -‐
![Page 14: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/14.jpg)
Comparison of the main storage types Storage types Key points Weak points Internal -‐ Low range price
-‐ Needs no special place to reserve -‐ Poor number of slots à Poor number of disks -‐ Non-‐evolving architecture -‐ Storage and server is the same -‐ Impossible to mutualize the storage
NAS -‐ Mutualized -‐ Theoricaly no disks limita>on -‐ Growing capacity -‐ Storage miroring possible -‐ Middle range price
-‐ Classical network technology -‐ Proprietary filesystem making hard the raw devices management -‐ Need of a secured place to store -‐ Heavy electrical power supply
SAN -‐ Mutualized -‐ Theoricaly no disks limita>on -‐ Growing capacity -‐ Storage miroring possible -‐ Dedicated network -‐ Quasi-‐unlimited throughput -‐ Mutualized backup
-‐ Expensive !!! -‐ Need of a secured place to store -‐ Heavy electrical power supply
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 14 -‐
![Page 15: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/15.jpg)
SAN IBM DS8700
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 15 -‐
![Page 16: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/16.jpg)
SAN Oracle-‐Sun Exadata
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 16 -‐
![Page 17: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/17.jpg)
HP – XP 24000
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 17 -‐
![Page 18: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/18.jpg)
EMC Symmetrix DMX-‐4
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 18 -‐
![Page 19: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/19.jpg)
Example of a SAN architecture
SAN
Switch A Switch B
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 19 -‐
Server A Server B Server C Server D Server E Server F
![Page 20: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/20.jpg)
Physical considera>ons linked with the storage
• The number of disks is directly corelated to the throughtput with no-‐cache
• The cache has to be sized to accelerate the OLTP
• The logical units need to be created taking care of the class storage used
• The number of servers connected has a direct impact when sizing the storage
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 20 -‐
![Page 21: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/21.jpg)
Vocabulary (1/3)
• I/O : Input/Output on a peripheral • I/O/s : I/O per second • I/O length : Size of an I/O in KB, MB • I/O Wait : Wait on a synchrone I/O having an impact on the process
un>l the I/O is resolved • I/O Throughput : Maximum number of I/Os and maximum throughput
in bytes per second, or combina>on of these two measures • SAN switch : Special equipment linking the SAN and the servers, with
another SAN, a backup robot • FC or Fibre Channel : Protocol used to communicate with the storage
when fibre is used to connect the storage elements • RAID or Redondant Array of Inexpensive Disks : Hardware or sonware
architecture made up of many disks to ensure data security and I/O throughput
• SAN Fabric : SAN + FC + switches
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 21 -‐
![Page 22: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/22.jpg)
Vocabulary (2/3) • SCSI or Small Computer System Interface : Communica>on standard
between the computer and its peripherals • iSCSI or Internet SCSI : Protocol used to connect peripherals like SANs
with servers. The SCSI layer is encapsulated in the TCP/IP layer and is based on the client (server) / server (SAN) architecture
• LUN or Logical Unit Number : Pointer to a storage device. Designate too the storage
• WWN or World Wide Name : Unique iden>fier of a LUN on a SAN network • Zoning : SAN spli`ng in mul>ple zones • I/O Latency : Time necessary to acquire the I/O • SSD or Solid-‐State Drive : Hardware storage made of flash-‐memory. Very
fast. Main problem : Time and intensive usage destroy cells making holes in the matrix. Expensive.
• HDD or Hard-‐Disk Drive : Mecanical device with disks and read/write heads. Less fat than the SSD, but reliable. Cheap. Main problem : Mecanical parts = Fragility
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 22 -‐
![Page 23: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/23.jpg)
Vocabulary (3/3) – The most common RAID
• RAID 0 : Disk striping à Disks agregate in the purpose to make a unique storage space, parallelizing reads and writes. No safety in case of a disk loss
• RAID 1 : Disk mirroring à 2 or more disks are miror of each other. Writes are made simultaneously on the 2 disks, reads are made by the faster of the miror
• RAID 5 : Disks agregate with parity between each disk. The loss of one disk doesn’t imply the loss of the en>re agregate. The lost disk can be hot unplugged and replaced immediatly by a new one who will be synchronized by the others
• RAID 6 : Mul>ple parity. It can be possible to loose more than one disk depending of the security level choosen. Parity n equals possible loss of n disks without problem
• RAID 0 + 1 : Priority for the RAID 0. One disk lost implies the loss of the en>re miror
• RAID 1 + 0 : Priority for the RAID 1. Much more robust. Only the loss of all disks of one side of the miror makes the loss of the miror
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 23 -‐
![Page 24: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/24.jpg)
Some SAN disks architectures
• To major architecture exist : – vRAID 6
– Clustered vRAID 5
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 24 -‐
Aggregate
![Page 25: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/25.jpg)
From the storage performance to the client queries performance
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 25 -‐
Storage
DB Server DB Server DB Server DB Server
Appli Server
Appli Server
Appli Server
Appli Server
Appli Server
Appli Server
Appli Server
![Page 26: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/26.jpg)
Distribu>on rules of the data on the storage
• The befer is to have aggregates with a wide number of disks to maximize I/O throughput
• The transac>on logs have to be on the faster wri>ng aggregates
• The data files need to be on the much more secure aggregates
• The distribu>on of the database files must follow the usage frequency of the data
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 26 -‐
![Page 27: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/27.jpg)
Complex architectures
• Replica>on of all or a part of a SAN • Real-‐>me replica>on between SANs • SAN SSD (mega-‐cache) over SAN HDD
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 27 -‐
![Page 28: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/28.jpg)
The server: DBMS « engine »
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 28 -‐
![Page 29: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/29.jpg)
IBM Power Server product line
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 29 -‐
![Page 30: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/30.jpg)
HP Integrity Servers product line
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 30 -‐
![Page 31: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/31.jpg)
Oracle-‐Sun product line
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 31 -‐
![Page 32: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/32.jpg)
Hardware guiding the server performance
• Memory • CPU • Network card • Storage card • Low-‐latency network card (specific to cluster) • Motherboard or cabinet
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 32 -‐
![Page 33: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/33.jpg)
Sonware guiding the server performance
• Opera>ng system, calibra>on: – Memory management – Semaphore management – System cache management – Different buffers – IPC – …
• Drivers : – Have the good versions!! – Prefer the vendor drivers than the generic drivers coming with the opera>ng system
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 33 -‐
![Page 34: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/34.jpg)
Server calibra>on when used for a RDBMS
• Choice of the hardware (PC-‐like or other) • Choice of the architecture (64bit generally) • … twice (Itanium, Opteron/Xeon) • Choice of the opera>ng system • Mul>ple CPUs and mul>ple cores • Memory sizing • No unused daemon started • Swap sizing • Good parameters for the opera>ng system for the RDBMS usage
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 34 -‐
![Page 35: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/35.jpg)
From the hardware architecture to the RDBMS
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 35 -‐
![Page 36: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/36.jpg)
Link between the RDBMS and the hardware
• The hardware provides: – The storage – The compu>ng power delivered by the server – The physical access through the server
• The RDBMS: – Leans on the server to process data
• An op>mal configura>on of a DBMS with th hardware has to be made depending on the applica>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 36 -‐
![Page 37: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/37.jpg)
What RDBMS for what usage?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 37 -‐
![Page 38: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/38.jpg)
MySQLTM (Oracle -‐ Sun) • Database of small to huge dimension • « Free » for a personal usage • Paying distribu>on when having to deploy on produc>ons and
dispose of a support • Cluster with a master in read/write mode and read mode slaves • InnoDB necessary to have the ACID proper>es of the DBMS • Very fast if using the MyISAM tables (no transac>onal capability!) • Mainly used for websites and now coming to cri>cal professional
websites and applica>ons with heavy transac>onal load • Present on an important choice of opera>ng systems
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 38 -‐
![Page 39: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/39.jpg)
PostgreSQL
• Database from small to very large size • « Free » for a personal usage • Support license • Cluster license • Fully ACID compliant • Can easily be compared to the Oracle RDBMS for its performance and agility
• Found on a great number of opera>ng systems • Exper>se in G.I.S.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 39 -‐
![Page 40: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/40.jpg)
SybaseTM ASE (Adap>ve Server Enterprise)
• Commercial sonware • Database small to heavy size • Internal management of the blocks with sorted lines (Index Organized Tables) na>vely
• Can be found on mul>ple systems • OLTP oriented even if some companies useit for decisional systems
• Clustering capabili>es • Simplified management administra>on • Data replica>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 40 -‐
![Page 41: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/41.jpg)
SybaseTM IQ
• Commercial sonware • Fully dedicated on decisional systems • The blocks contain columns and these columns are systema>cally indexed
• To be used with very huge databases • Fully ACID compliant • Can be found on mul>ple opera>ng systems • Clustering capabili>es
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 41 -‐
![Page 42: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/42.jpg)
OracleTM RDBMS • Commercial sonware • Polyvalent engine (OLTP, Decisional, Hybrid) • From small to very huge databases • Extremely adap>ve making it very complex to parameter
• Fully ACID compliant • Can be found on most of the opera>ng systems • Advanced cluster capabili>es (leader) • Advanced disaster recovery management • Advanced replica>on func>onnali>es
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 42 -‐
![Page 43: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/43.jpg)
IBMTM DB2TM UDBTM • Commercial sonware • Polyvalent engine (OLTP, Decisional, Hybride) • From small to very huge size • Extremely adap>ve making it complex to manage • Fully ACID compliant • Can be found on most of the opera>ng systems • Clustering capabili>es (splifed databases) • Disaster recovery capabili>es • Replica>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 43 -‐
![Page 44: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/44.jpg)
MicrosonTM SQL ServerTM • Commercial sonware • Born with the end of the collabora>on with Sybase in version 5.
Microson decided to make its own evolu>on of the engine • From small to heavy databases • Same block management as Sybase ASE (sorted lines inside the
blocks) • ONLY on Windows • Essen>ally dedicated to OLTP • Fully ACID compliant • Clustering capabili>es • Easy management • Replica>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 44 -‐
![Page 45: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/45.jpg)
Conclusion • The DBMS are extremely dependant of the hardware architecture
• During to concep>on phase of a new applica>on on a new architecture, befer is the knowledge of the architecture, befer is the sizing of the architecture to comply with the needs of the applica>on
• If it concerns an applica>ve evolu>on, the performance data from the actual produc>on can permit to an>cipate on the sizing of the new architecture
• The physical architecture have to be taken in considera>on during the applica>on crea>on, to be sure that the applica>on will be efficient
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 45 -‐
![Page 46: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/46.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 46 -‐
![Page 47: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/47.jpg)
The conceptual model
• The nearest of the human • It’s not directly linked to a par>cular database kernel
• It’s based on the en>ty-‐rela>onship concept
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 47 -‐
![Page 48: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/48.jpg)
En>ty-‐Rela>onship
• An en>ty is a data collec>on • A rela>onship is a link between two data collec>ons
• It’s par>cularly based on the Boyce-‐Codd normal form, and especially on the normal form key domain
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 48 -‐
![Page 49: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/49.jpg)
Normal Form
• It’s a data structura>on • This form is normalized : It follows rules • The goal is to normalize the data • The normaliza>on is physically limited to a unique database
• The normal forms as defined in the rela>onal model are 6
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 49 -‐
![Page 50: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/50.jpg)
Role of the normal forms
• Avoid the transac>onal anomalies: – Data redundancy – Read anomaly – Write anomaly – Counter-‐performance
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 50 -‐
![Page 51: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/51.jpg)
First Normal Form – 1NF (source Wikipedia)
• First normal form (1NF) is a property of a rela>on in a rela>onal database. A rela>on is in first normal form if the domain of each afribute contains only atomic values, and the value of each afribute contains only a single value from that domain.
• Edgar Codd, in a 1971 conference paper, defined a rela>on in first normal form to be one such that none of the domains of that rela>on should have elements which are themselves sets.
• First normal form is an essen>al property of a rela>on in a rela>onal database. Database normaliza>on is the process of represen>ng a database in terms of rela>ons in standard normal forms, where first normal is a minimal requirement.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 51 -‐
![Page 52: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/52.jpg)
Second Normal Form – 2NF (source Wikipedia)
• Second normal form (2NF) is a normal form used in database normaliza>on. 2NF was originally defined by E.F. Codd in 1971.
• A table that is in first normal form (1NF) must meet addi>onal criteria if it is to qualify for second normal form. Specifically: a table is in 2NF if and only if it is in 1NF and no non-‐prime afribute is dependent on any proper subset of any candidate key of the table. A non-‐prime afribute of a table is an afribute that is not a part of any candidate key of the table.
• Put simply, a table is in 2NF if and only if it is in 1NF and every non-‐prime afribute of the table is dependent on the whole of a candidate key.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 52 -‐
![Page 53: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/53.jpg)
Third Normal Form – 3NF (source Wikipedia)
• The third normal form (3NF) is a normal form used in database normaliza>on. 3NF was originally defined by E.F. Codd in 1971. Codd's defini>on states that a table is in 3NF if and only if both of the following condi>ons hold:
• The rela>on R (table) is in second normal form (2NF) • Every non-‐prime afribute of R is non-‐transi>vely dependent (i.e. directly dependent) on
every superkey of R. • A non-‐prime afribute of R is an afribute that does not belong to any candidate key of R. A
transi>ve dependency is a func>onal dependency in which X → Z (X determines Z) indirectly, by virtue of X → Y and Y → Z (where it is not the case that Y → X).
• A 3NF defini>on that is equivalent to Codd's, but expressed differently, was given by Carlo Zaniolo in 1982. This defini>on states that a table is in 3NF if and only if, for each of its func>onal dependencies X → A, at least one of the following condi>ons holds:
• X contains A (that is, X → A is trivial func>onal dependency), or • X is a superkey, or • Every element of A-‐X, the set difference between A and X, is a prime afribute (i.e., each
afribute in A-‐X is contained in some candidate key)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 53 -‐
![Page 54: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/54.jpg)
Boyce-‐Codd Normal Form -‐ BCNF (source Wikipedia)
• Boyce–Codd normal form (or BCNF or 3.5NF) is a normal form used in database normaliza>on. It is a slightly stronger version of the third normal form (3NF). BCNF was developed in 1974 by Raymond F. Boyce and Edgar F. Codd to address certain types of anomaly not dealt with by 3NF as originally defined.
• Chris Date has pointed out that a defini>on of what we now know as BCNF appeared in a paper by Ian Heath in 1971. Date writes:
• "Since that defini>on predated Boyce and Codd's own defini>on by some three years, it seems to me that BCNF ought by rights to be called Heath normal form. But it isn't."
• If a rela>onal scheme is in BCNF then all redundancy based on func>onal dependency has been removed, although other types of redundancy may s>ll exist. A rela>onal schema R is in Boyce–Codd normal form if and only if for every one of its dependencies X → Y, at least one of the following condi>ons hold:
• X → Y is a trivial func>onal dependency (Y ⊆ X) • X is a superkey for schema R
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 54 -‐
![Page 55: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/55.jpg)
Fourth normal form – 4NF (source Wikipedia)
• Fourth normal form (4NF) is a normal form used in database normaliza>on.
• Introduced by Ronald Fagin in 1977, 4NF is the next level of normaliza>on aner Boyce–Codd normal form (BCNF).
• Whereas the second, third, and Boyce–Codd normal forms are concerned with func>onal dependencies, 4NF is concerned with a more general type of dependency known as a mul>valued dependency.
• A Table is in 4NF if and only if, for every one of its non-‐trivial mul>valued dependencies X \twoheadrightarrow Y, X is a superkey—that is, X is either a candidate key or a superset thereof.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 55 -‐
![Page 56: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/56.jpg)
Finh Normal Form – 5NF (source Wikipedia)
• Finh normal form (5NF), also known as project-‐join normal form (PJ/NF) is a level of database normaliza>on designed to reduce redundancy in rela>onal databases recording mul>-‐valued facts by isola>ng seman>cally related mul>ple rela>onships. A table is said to be in the 5NF if and only if every join dependency in it is implied by the candidate keys.
• A join dependency *{A, B, … Z} on R is implied by the candidate key(s) of R if and only if each of A, B, …, Z is a superkey for R.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 56 -‐
![Page 57: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/57.jpg)
Sixth Normal Form – 6NF (source Wikipedia)
• Sixth normal form (6NF) is a term in rela>onal database theory, used in two different ways. • A book by Christopher J. Date and others on temporal databases, defined sixth normal form as a normal
form for databases based on an extension of the rela>onal algebra. • In this work, the rela>onal operators, such as join, are generalized to support a natural treatment of
interval data, such as sequences of dates or moments in >me. • Sixth normal form is then based on this generalized join, as follows:
– A relvar R [table] is in sixth normal form (abbreviated 6NF) if and only if it sa>sfies no nontrivial join dependencies at all — where, as before, a join dependency is trivial if and only if at least one of the projec>ons (possibly U_projec>ons) involved is taken over the set of all afributes of the relvar [table] concerned.
• Any rela>on in 6NF is also in 5NF. • Sixth normal form is intended to decompose rela>on variables to irreducible components. Though this
may be rela>vely unimportant for non-‐temporal rela>on variables, it can be important when dealing with temporal variables or other interval data. For instance, if a rela>on comprises a supplier's name, status, and city, we may also want to add temporal data, such as the >me during which these values are, or were, valid (e.g., for historical data) but the three values may vary independently of each other and at different rates. We may, for instance, wish to trace the history of changes to Status.
• For further discussion on Temporal Aggrega>on in SQL, see also Zimanyi. For a different approach, see TSQL2.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 57 -‐
![Page 58: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/58.jpg)
Domain-‐Key Normal Form -‐ FNDC (source Wikipedia)
• Domain/key normal form (DKNF) is a normal form used in database normaliza>on which requires that the database contains no constraints other than domain constraints and key constraints.
• A domain constraint specifies the permissible values for a given afribute, while a key constraint specifies the afributes that uniquely iden>fy a row in a given table.
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 58 -‐
![Page 59: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/59.jpg)
MySQLWorkbench
• The tool • Exercice
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 59 -‐
![Page 60: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/60.jpg)
Data model example
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 60 -‐
![Page 61: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/61.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 61 -‐
![Page 62: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/62.jpg)
The HTML
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 62 -‐
![Page 63: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/63.jpg)
What’s the HTML
• HTML is derived from SGML • HTML is the acronym for HyperText Markup Language
• HTML permits to define the content of a WEB page with links to other pages
• The main program to display HTML content is a WEB browser
• That WEB browser can be a CLI like Lynx
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 63 -‐
![Page 64: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/64.jpg)
Markups
• Like in XML, the markups have a par>cular syntax with a begin markup and (not all >me) an end markup:
<keyword></keyword> <keyword /> <keyword> (Correct… but… Bad!!!) • If a keyword has no end markup, prefer to use the syntax “<keyword />” with the “/” indica>ng the end of the markup
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 64 -‐
![Page 65: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/65.jpg)
Generic structure of an HTML page
<!doctype html> <html> <head> </head> <body> </body> </html>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 65 -‐
![Page 66: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/66.jpg)
What is HTML?
• HTML is made to display informa>on on a terminal
• HTML doesn’t need, normally, to format the presenta>on of the text
• HTML places objects but doesn’t have to format them
• CSS is made for that
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 66 -‐
![Page 67: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/67.jpg)
Head of an HTML page <head> </head> • This is the part where we define: – Character set – Metadata – Javascript programs – CSS – Title of the page
• No informa>on except the >tle of the page will be displayed in the WEB browser
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 67 -‐
![Page 68: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/68.jpg)
Character set
• Most important part of a document • Defines the coding of the page • Needs to be chosen with care • One’s it’s chosen, it has to be propagated to all the coding levels and data sources
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 68 -‐
![Page 69: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/69.jpg)
Metadata
• Used to pass variables to the browser, like character set…
• Used to indicate some informa>ons for the search engines like Google, Bing, Yahoo!
• To link to another page with a >mer (Refresh) • … and many other things
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 69 -‐
![Page 70: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/70.jpg)
Body of an HTML Page <body> </body> • This part contains all the page content:
– Text – Images – Mul>media
• The content is ordered with: – Divs – Spans – Tables (try to forget it. Prefer the use of divs) – Headers – Paragraphs
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 70 -‐
![Page 71: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/71.jpg)
Comments
<!-‐-‐ [...] -‐-‐> • Use this markup to introduce comments inside a page
• Can be used to deac>vate temporarily a piece of HTML code
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 71 -‐
![Page 72: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/72.jpg)
Divs
<div> </div> • This is the main container for all the objects • It’s the befer way to order them
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 72 -‐
![Page 73: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/73.jpg)
Spans
<span> </span> • This object is used to place par>cular formats for the objects
• It can be used to place a special class for displaying informa>on in a par>cular way
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 73 -‐
![Page 74: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/74.jpg)
Tables <table> <thead> </thead> <tbody> <tr> <td> </td> </tr> </tbody> <�ooter> </�ooter> </table> • Tables are less used with the succession of the divs • Some>mes the tables are more adapted than the divs (accountancy
table…)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 74 -‐
![Page 75: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/75.jpg)
Headers
<h1></h1> <h2></h2> […] <h6></h6> • Headers are used for the chapters of the content, like in the books
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 75 -‐
![Page 76: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/76.jpg)
Paragraphs
<p> </p> • A paragraph is an object to format text like in the books
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 76 -‐
![Page 77: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/77.jpg)
Address or Hyperlink and Names <a href="[URL]">[text]</a> • The address is an hyperlink to another page or to an element in the page
<a name="[value]">[text]</a> • Examples: <a href="hfp://www.w3.org/">W3C</a> <a href="hfp://www.mysite.org/index.php#introduc>on">Introduc>on</a> <a name="introduc>on">1. Introduc>on</a>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 77 -‐
![Page 78: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/78.jpg)
Lists
<ol></ol>: ordered list (with numbers) <ul></ul>: unordered list (with bullets) <li></li> • Create lists of values • Used to create menus in combina>on with CSS
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 78 -‐
![Page 79: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/79.jpg)
Images
<img src="URL" /> • Insert an image in a page
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 79 -‐
![Page 80: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/80.jpg)
Forms (1)
<form ac>on="[...]" method="[get|post]"> </form> • Forms are used to pass data to the server • They contain fields and valida>on/cancella>on bufons
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 80 -‐
![Page 81: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/81.jpg)
Forms input + label (2) <input type="[type]" name="[name]" id="[id]" value="[value]"> • … where type is:
– text – date, date>me – password – radio – checkbox – submit – color – email – file – hidden – image – month – number – range – search – tel – url – week
<label for="[id]">Text of the label</label> • Defines a label for an input field
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 81 -‐
![Page 82: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/82.jpg)
Forms textarea (3)
<textarea name="[name]" id="[id]" rows="[number of rows]" cols="[number of columns]">[your text]</textarea> • Defines a field to enter a long text
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 82 -‐
![Page 83: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/83.jpg)
Input select (4)
<select name="[name]" id="[id]" [mul>ple="mul>ple"] [size="number of rows displayed"]> <op>on [default="default"] value="[value]">text op>on</op>on> </select> • This field is made to choose one or more (mul>ple) values in a list
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 83 -‐
![Page 84: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/84.jpg)
Form fieldset (5)
<fieldset>[list of fields]</fieldset> • Group fields in a box for styling purpose
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 84 -‐
![Page 85: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/85.jpg)
Form bufon (6)
<bufon type="bufon" name="[name]" id="[id]" value="[value]">Your text</bufon>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 85 -‐
![Page 86: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/86.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 86 -‐
![Page 87: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/87.jpg)
What is PHP?
• PHP is the acronym for "PHP: Hypertext processor"
• This is a scrip>ng language made for WEB development
• It can be embedded inside HTML • Easy to learn • Taking part of C, Java and Perl
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 87 -‐
![Page 88: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/88.jpg)
Making a PHP program <?php echo "blablabla"; ?> This can be <?php echo "a program"; ?> wrifen <?php echo "inside HTML"; ?> • These two tags can be used to do a php program file • …or embedded inside HTML
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 88 -‐
![Page 89: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/89.jpg)
Semi-‐column instruc>ons separator
echo "blabla"; • As in C or Java, the semi-‐column is the instruc>ons separator
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 89 -‐
![Page 90: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/90.jpg)
Variables
$var_c = "blabla"; $var_i = 1; • PHP is a light-‐typed language
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 90 -‐
![Page 91: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/91.jpg)
Variables
$a = "1"; $b = $a + 1; $c = 2; $d = $c + "15 pieces";
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 91 -‐
![Page 92: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/92.jpg)
Par>cular variables
• $_POST: all the post values of a form with method post
• $_GET: all the get values of a form with method get
• $_SERVER: can return any server variable to use in the program such as "DOCUMENT_ROOT" who is the root of all the PHP code you use
• $_COOKIE: to use the cookies of the browser
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 92 -‐
![Page 93: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/93.jpg)
Operators
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 93 -‐
![Page 94: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/94.jpg)
Operator precedence
• See: hfp://www.php.net/manual/en/language.operators.precedence.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 94 -‐
![Page 95: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/95.jpg)
Arithme>c operators
• See: hfp://www.php.net/manual/en/language.operators.precedence.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 95 -‐
![Page 96: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/96.jpg)
Assignment operators • Assignment
– Symbol "=": $a = 3; $b = $a * (3 + $a); $c = "abcd";
– Symbol "<operator>=": $a += 1; $b *= $a; $d .= "efgh"; • Assignment by reference $a = 3; $b = &$a; // $a and $b are poin>ng on the same data Modifying $a or $b modifies either of two
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 96 -‐
![Page 97: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/97.jpg)
Bitwise operators
• They are used only on integers $a & $b; // And $a | $b; // Or $a ^ $b; // Xor ~ $a ; // Not $a << $b; // Shin len. $a is shined len by $b bits $a >> $b; // Shin right. $a is shined right by $b bits
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 97 -‐
![Page 98: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/98.jpg)
Control operators $a == $b; // Equality $a === $b; // Iden>ty $a != $b; // Non-‐equality $a <> $b; // Non-‐equality $a !== $b; // Non-‐iden>ty $a < $b; // Less than $a > $b; // Greater than $a <= $b; // Less or equal $a >= $b; // Greater or equal
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 98 -‐
![Page 99: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/99.jpg)
Incremen>ng/Decremen>ng operators
++$a; // Pre-‐increment $a++; // Post-‐increment -‐-‐$a; // Pre-‐decrement $a-‐-‐; // Post-‐decrement
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 99 -‐
![Page 100: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/100.jpg)
Logical operators
$a and $b; // TRUE if $a and $b are TRUE $a && $b; // same as preceding $a or $b; // TRUE if either $a or $b are TRUE $a || $b; // same as preceding $a xor $b; // TRUE if one or the other is TRUE, but not both ! $a; // TRUE if $a is not TRUE. FALSE if $a is not FALSE
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 100 -‐
![Page 101: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/101.jpg)
String operators
$a = "This is a "; $b = $a . "concatenated string."; // $b is "This is a concatenated string." $a = "This is a "; $b .= "concatenated string."; // $b is "This is a concatenated string."
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 101 -‐
![Page 102: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/102.jpg)
Array operators
$a + $b; // Union of $a and $b $a == $b; // TRUE if $a and $b have the same key/value pairs $a === $b; // TRUE if $a and $b have the same key/value pairs in the same order and the same types $a != $b; // TRUE if $a is not equal to $b $a <> $b; // TRUE if $a is not equal to $b $a !== $b; // TRUE if $a is not iden>cal to $b
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 102 -‐
![Page 103: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/103.jpg)
Control structures
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 103 -‐
![Page 104: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/104.jpg)
if
if(<expr>) <statement>
where <statement> is: <instruc>on>;
or {
<statements> } Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 104 -‐
![Page 105: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/105.jpg)
else
if(<expr>){ <statements>
} else { <statements>
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 105 -‐
![Page 106: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/106.jpg)
else if/elseif
if(<expr>){ <statements>
} elseif(<expr>) { <statements>
} else { <statements>
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 106 -‐
![Page 107: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/107.jpg)
while
while(<expr>) <statements>
or while(<expr>):
<statements> endwhile;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 107 -‐
![Page 108: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/108.jpg)
do-‐while
do <statements>
while(<expr>);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 108 -‐
![Page 109: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/109.jpg)
for
for(<define var>;<expr>;<modify var>) <statements>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 109 -‐
![Page 110: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/110.jpg)
foreach
foreach(<array_expression> as $value) <statements>
or foreach(<array_expression as $key => $value)
<statements>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 110 -‐
![Page 111: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/111.jpg)
break
• This instruc>on is used to exit a control structure
• Used with a parameter exit the number of control structures corresponding to the value
break; break 2; // Exit two levels
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 111 -‐
![Page 112: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/112.jpg)
con>nue
• Used to con>nue to the next loop of a control structure
• The instruc>ons following "con>nue" are skipped
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 112 -‐
![Page 113: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/113.jpg)
switch switch(<variable>){
case <value1>: <statements> [break;] case <value2>: <statements> [break;] [default: <statements> ]
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 113 -‐
![Page 114: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/114.jpg)
return • Inside a php file is used to con>nue with the code who contains an include of the
file a.php <?php include("b.php"); echo "a"; ?> b.php <?php echo "b"; return; ?> … execu>ng a.php will display "ba"
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 114 -‐
![Page 115: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/115.jpg)
include/require
• "include" in case of failure will produce a E_WARNING
• "require" in case of failure will produce a E_COMPILE_ERROR
include("a.php"); require("b.php");
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 115 -‐
![Page 116: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/116.jpg)
include_once/require_once
• Same as include/require but never include/require another >me a file when it was already included/required
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 116 -‐
![Page 117: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/117.jpg)
Func>ons
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 117 -‐
![Page 118: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/118.jpg)
User-‐defined func>ons
func>on <name>(<arg1>, … , <argn>){ <statements>
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 118 -‐
![Page 119: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/119.jpg)
Func>on arguments
• That can be an array • By default is passed by value • With "&" the argument is passed by reference • To have a default value just do "$var = <value>"
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 119 -‐
![Page 120: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/120.jpg)
Returning value
return <value>; • The <value> is returned to the variable associated with the func>on
func>on a(){ return 10;
} $var = a(); // $var will be 10
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 120 -‐
![Page 121: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/121.jpg)
Variable func>ons
• A variable func>on is a func>on who is called with the evalua>on of a variable
$func = "test"; $func(10); func>on test($a){
$b = $a + 10; return($b);
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 121 -‐
![Page 122: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/122.jpg)
Classes and Objects
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 122 -‐
![Page 123: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/123.jpg)
Defining a basic class
class BasicClass{ public $var1 = "My var one "; public $var2 = 2;
public func>on myFunc>on(){ return($this-‐>$var1.$this-‐>$var2); }
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 123 -‐
![Page 124: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/124.jpg)
Proper>es of a class • Proper>es are called "afributes" in other OOP languages
class BasicClass{ public $var1 = "My var one "; public $var2 = 2;
public func>on myFunc>on(){ return($this-‐>$var1.$this-‐>$var2); }
} Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 124 -‐
![Page 125: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/125.jpg)
Class constants class BasicClass{
const PI = 3.1415926535; public $var1 = " is Pi ";
public func>on myFunc>on(){ return(self::PI.$this-‐>$var1); }
} echo BasicClass::PI;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 125 -‐
![Page 126: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/126.jpg)
Autoloading classes
• The goal of this feature is to load automa>cally the classes defined in a php file
func>on __autoload($class_name){ include $class_name.".php";
} $a = new FirstClass(); $b = new SecondClass();
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 126 -‐
![Page 127: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/127.jpg)
Constructor and destructor void __construct ([ mixed $args [, $... ]] ) class BaseClass { func>on __construct() { print "In BaseClass constructor\n"; } } void __destruct ( void ) class MyDestructableClass { func>on __construct() { print "In constructor\n"; $this-‐>name = "MyDestructableClass"; } func>on __destruct() { print "Destroying " . $this-‐>name . "\n"; } }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 127 -‐
![Page 128: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/128.jpg)
Visibility
• public: any method/property defined as public is always visible from anywhere
• protected: any method/property defined as protected can be accessed only within the class itself and by inherited and parent classes
• private: any method/property defined as private may only be accessed by the class
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 128 -‐
![Page 129: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/129.jpg)
Scope resolu>on operator … So called "double colon" class BasicClass{
const PI = 3.1415926535; public sta_c $my_stat = 2; public $var1 = " is Pi ";
public func>on myFunc>on(){ return(self::PI.$this-‐>$var1); }
} echo BasicClass::PI; echo BasicClass::$my_stat;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 129 -‐
![Page 130: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/130.jpg)
Sta>c class BasicClass{
const PI = 3.1415926535; public sta_c $my_stat = 2; public $var1 = " is Pi ";
public sta>c func>on myFunc>on(){ return(self::PI.$this-‐>$var1); }
} echo BasicClass::$my_stat; BasicClass::myFunc_on(); $classname = "BasicClass"; $classname::myFunc_on();
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 130 -‐
![Page 131: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/131.jpg)
Class abstrac>on (1) • An abstract class may not be instan>ated • It can be only extended • Can't contain private method/property • All abstract method/property as to be defined in the child class
• If a method/property is protected it can be defined protected or public
• The signatures of the methods must match • Op>onal arguments can be defined in the child class
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 131 -‐
![Page 132: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/132.jpg)
Class abstrac>on (2) abstract class AbstractBasicClass{
abstract protected sta>c $my_stat; abstract public $var1;
abstract public sta>c func>on myFunc>on($a);
} class BasicClass extends AbstractBasicClass{
public sta_c $my_stat = 2; public $var1 = " as a result.";
public sta>c func>on myFunc>on($a,$b = 10){ return(($a * $b.$this-‐>$var1); }
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 132 -‐
![Page 133: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/133.jpg)
Object interface • All methods declared in an interface must be public interface iBasicTemplate{
public sta>c func>on myFunc>on(); } class BasicClass implements iBasicTemplate{
const PI = 3.1415926535; public sta_c $my_stat = 2; public $var1 = " is Pi ";
public sta>c func>on myFunc>on(){ return(self::PI.$this-‐>$var1); }
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 133 -‐
![Page 134: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/134.jpg)
Traits
• Traits is as a class but not a class trait myFunc>ons{
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 134 -‐
![Page 135: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/135.jpg)
Overloading
• Overloading is used to dynamically manage methods through magic methods
public void __set ( string $name , mixed $value ) public mixed __get ( string $name ) public bool __isset ( string $name ) public void __unset ( string $name )
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 135 -‐
![Page 136: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/136.jpg)
Object itera>on • Browse the proper>es of a class class BasicClass{
public $var1 = "My var one "; public $var2 = 2; private $var3 = "10 items"; protected $var4 = "protected property";
func>on iterateVisible() { echo "MyClass::iterateVisible:\n"; foreach($this as $key => $value) { print "$key => $value\n"; } } $class = new BasicClass(); foreach($class as $key => $value) { print "$key => $value\n"; } $class-‐>iterateVisible();
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 136 -‐
![Page 137: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/137.jpg)
Final keyword • The keyword "final" is used to prevent a child class to override the defini>on of a method
class BasicClass{ public $var1 = "My var one "; public $var2 = 2;
final public func>on myFunc>on(){ return($this-‐>$var1.$this-‐>$var2); }
} Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 137 -‐
![Page 138: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/138.jpg)
Object cloning void __clone(void) • This method is used to clone all or part of an object • This method can't be called directly class SubClass{
sta>c $instances = 0; public $instance; public func>on __construct(){ $this-‐>instance = ++self::$instances; } public func>on __clone(){ $this-‐>instance = ++self::$instances; }
} class BasicClass{
public $obj1; public $obj2; func>on clone(){ $this-‐>obj1 = clone $this-‐>obj1; }
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 138 -‐
$obj = new BasicClass(); $obj-‐>obj1 = new SubClass(); $obj-‐>obj2 = new SubClass(); $obj2 = clone $obj; • $obj will contain:
• $obj2-‐>obj1-‐>instance: 1 • $obj2-‐>obj2-‐>instance: 2
• $obj2 will contain:
• $obj2-‐>obj1-‐>instance: 3 • $obj2-‐>obj2-‐>instance: 2
![Page 139: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/139.jpg)
Comparing objects
• The comparison operators to use are: – === – !==
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 139 -‐
![Page 140: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/140.jpg)
Sta>c keyword class A{
public sta>c func>on p(){ echo __CLASS__; } public sta>c func>on test(){ self::p(); }
} class B extends A{
public sta>c func>on p(){ echo __CLASS__; }
} B::test(); // will produce "A"
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 140 -‐
class A{ public sta>c func>on p(){ echo __CLASS__; } public sta>c func>on test(){ sta>c::p(); }
} class B extends A{
public sta>c func>on p(){ echo __CLASS__; }
} B::test(); // will produce "B"
![Page 141: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/141.jpg)
Namespaces
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 141 -‐
![Page 142: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/142.jpg)
What are namespaces?
• That's almost the same as a filesystem representa>on
• It permits to have the same name for objects with different files using a nota>on indica>ng an absolute or rela>ve reference to the object
namespace N1 N1\N2\$obj; //$obj in N1\N2 $obj; // $obj in N1 (not the same) N2\$obj; // $obj in N1\N2 (same as first)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 142 -‐
![Page 143: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/143.jpg)
Declaring namespaces and sub-‐namespaces
• Declaring a namespace: namespace N1; • Declaring sub-‐namespace: namespace N1\N2;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 143 -‐
![Page 144: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/144.jpg)
__NAMESPACE__ keyword
• __NAMESPACE__ keyword returns the current namespace
• It can be used to dynamically address objects with
namespace N1; func>on get($classname){
$c = __NAMESPACE__."\\".$classname; return new $c;
} Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 144 -‐
![Page 145: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/145.jpg)
Using global space
namespace N1; func>on fopen(){
$f = \fopen(…); }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 145 -‐
![Page 146: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/146.jpg)
Name resolu>on rules
• See documenta>on: hfp://www.php.net/manual/en/language.namespaces.rules.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 146 -‐
![Page 147: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/147.jpg)
Excep>ons handling
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 147 -‐
![Page 148: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/148.jpg)
How to handle excep>ons
try{ // code
} catch(e) { } finally { }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 148 -‐
![Page 149: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/149.jpg)
Generator
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 149 -‐
![Page 150: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/150.jpg)
Generator
• Simplifies the iterators that are normally wrifen inside a class
• One keyword does the whole work: yield
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 150 -‐
![Page 151: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/151.jpg)
Yield
• "yield" acts as "return" but just returns the current value and con>nue the loop where it is
Sample: hfp://www.php.net/manual/en/language.generators.overview.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 151 -‐
![Page 152: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/152.jpg)
Predefined variables
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 152 -‐
![Page 153: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/153.jpg)
$GLOBALS
• That variable is an array containing all the variables defined in the global scope of the current script
$a = 10; func>on test(){
$a = "abcd"; echo $GLOBALS["a"]; // 10 echo $a; // "abcd"
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 153 -‐
![Page 154: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/154.jpg)
$_SERVER
• This array contains all the environment variables defined at server level
• See: hfp://www.php.net/manual/en/reserved.variables.server.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 154 -‐
![Page 155: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/155.jpg)
$_GET
• This array contains the values send through the URL by a form or a link
hfp://myurl/myfile.php?a=10&b=azoirjaorha $_GET["a"] à 10 $_GET["b"] à "azoirjaorha"
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 155 -‐
![Page 156: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/156.jpg)
$_POST
• Same as $_GET but parameters can only be sent through a form
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 156 -‐
![Page 157: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/157.jpg)
$_COOKIE
• This array contains all the cookies manipulated by the server
$ok_registered = $sql_ac-‐>verifCookies(base64_decode($_COOKIE["gp"]),base64_decode($_COOKIE["tq"]));
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 157 -‐
![Page 158: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/158.jpg)
$REQUEST
• A super-‐global array containing: – $_GET – $_POST – $_COOKIE
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 158 -‐
![Page 159: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/159.jpg)
$_FILE • This array contains all the informa>ons about the files to upload on the server, and the file(s) itself(themselves) func>on telechargerFichier($composant,$taille_max){ if(!empty($_FILES["fichier_file"]["tmp_name"]) && is_uploaded_file($_FILES["fichier_file"]["tmp_name"])){ if(filesize($_FILES["fichier_file"]["tmp_name"]) < $taille_max * 1024){ //$type = exif_imagetype($_FILES["media_file"]["tmp_name"]); $fichier_dir = $this-‐>getInternalDirectory($composant); if(move_uploaded_file($_FILES["fichier_file"]["tmp_name"],$fichier_dir.$_FILES["fichier_file"]["name"])){ return(sprin�("Transfer OK : %s",$fichier_dir.$_FILES["fichier_file"]["name"])); }else{ return(sprin�("Transfer problem %s to %s",$_FILES["fichier_file"]["tmp_name"],$fichier_dir."/".$_FILES["fichier_file"]["name"])); } }else{ return(sprin�("File too big: %d Ko. Maximum authorized size: %d Ko.",filesize($_FILES["fichier_file"]["tmp_name"]),$taille_max)); } }else{ return("Empty file or not loaded"); } }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 159 -‐
![Page 160: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/160.jpg)
$_SESSION
• Contains all the session variables, including session cookies
• See special func>ons to manipulate the session variables: hfp://www.php.net/manual/en/ref.session.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 160 -‐
![Page 161: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/161.jpg)
$_ENV
• Contains all the environment variables of the server
• Depending on the server it's impossible to list these variables. See each server documenta>on for the accessible variables
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 161 -‐
![Page 162: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/162.jpg)
$php_errormsg
• This variable contains the last error message generated by PHP
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 162 -‐
![Page 163: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/163.jpg)
Database security
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 163 -‐
![Page 164: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/164.jpg)
Design of the database • Separate clearly the applica>on logic from the data manipula>on logic
• Use the good grants for the users such as read-‐only
• Use views instead of tables for the queries • Don't allow connec>on ability from the client. Manage it server-‐side
• Encrypt cri>cal data with modules such as mcrypt, mhash
• Protect from SQL injec>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 164 -‐
![Page 165: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/165.jpg)
Protect from SQL injec>on
• Don't use directly the informa>ons sent by the server. That's the major security failure!
• Verify the informa>on sent by the client before use it in a statement
Use bind variables!!
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 165 -‐
![Page 166: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/166.jpg)
And to con>nue…
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 166 -‐
![Page 167: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/167.jpg)
• Definitely look the documenta>on! • Func>on reference: hfp://www.php.net/manual/en/funcref.php
• Security: hfp://www.php.net/manual/en/security.php
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 167 -‐
![Page 168: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/168.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 168 -‐
![Page 169: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/169.jpg)
PDO
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 169 -‐
![Page 170: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/170.jpg)
What is PDO? • PDO is the acronym for PHP Database Objects • PDO is made to handle database objects: – Tables – Views – Rows – …
• It provides an abstrac>on layer to access any database kernel and offers specific methods and proper>es for each par>cular kernel
• PDO is a PHP class
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 170 -‐
![Page 171: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/171.jpg)
Open and close a connec>on
• $db = new PDO(<connec>on string>,$user,$password);
$db = new PDO('mysql:host=localhost;dbname=groupe10',$user,$password); […] // Open the connec>on $db = null; // Close the connec>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 171 -‐
![Page 172: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/172.jpg)
Transac>ons
• To manage a transac>on, three instruc>ons: – PDO::beginTransac>on() – PDO::commit(); – PDO::rollBack();
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 172 -‐
![Page 173: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/173.jpg)
Preparing a query
• Use ONLY prepared statements!! • PDO::prepare(<statement>,[<op>ons>]) • PDO::bindParam(<param_name>,<value>) • PDO::execute([array(<parameters>)])
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 173 -‐
![Page 174: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/174.jpg)
Preparing a query examples • Example 1: $stmt = $db-‐>prepare("select col1,col2 from tab1 where col3 = :name"); $param[":name"] = "test"; $stmt-‐>execute($param); • Example 2: $stmt = $db-‐>prepare("select col1,col2 from tab1 where col3 = :name"); $stmt-‐>bindParam(":name","test"); $stmt-‐>execute(); Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 174 -‐
![Page 175: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/175.jpg)
Fetching resultset
• PDO::fetch(<fetch_style>[,<cursor_orienta>on>[,<cursor_offset>]])
$stmt = $db-‐>prepare("select col1,col2 from tab1 where col3 = :name"); $param[":name"] = "test"; $stmt-‐>execute($param); while($row = $stmt-‐>fetch(PDO::FETCH_ASSOC,PDO::FETCH_ORI_NEXT){
prin�("%s %s<br />",$row["col1"],$row["col2"]; }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 175 -‐
![Page 176: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/176.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax and jQuery – Using the database with jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 176 -‐
![Page 177: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/177.jpg)
MySQL
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 177 -‐
![Page 178: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/178.jpg)
SQL Language
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 178 -‐
![Page 179: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/179.jpg)
The SQL
• … is the acronym for Structured Query Language • Uses classical words to express queries against databases
• Can be more or less rich depending on the database kernel
• Is a set of instruc>ons classified in two major groups : – Data Defini>on Language (DDL) – Data Manipula>on Language (DML)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 179 -‐
![Page 180: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/180.jpg)
Data Defini>on Language
• All the instruc>ons to be able to create: – A database – A tablespace – A table – An index…
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 180 -‐
![Page 181: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/181.jpg)
Data Manipula>on Language
• All the instruc>ons to be able to: – Insert data into tables – Update data into tables – Delete data into tables – Select data
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 181 -‐
![Page 182: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/182.jpg)
Data Defini_on Language (DDL)
Copyright 2013 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 182 -‐
![Page 183: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/183.jpg)
Instruc>ons
• CREATE TABLE • CREATE INDEX • CREATE VIEW • CREATE TABLESPACE • See: hfp://dev.mysql.com/doc/refman/5.6/en/sql-‐syntax-‐data-‐defini>on.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 183 -‐
![Page 184: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/184.jpg)
Data Manipula_on Language (DML)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 184 -‐
![Page 185: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/185.jpg)
The instruc>ons
• ... are: – SELECT – INSERT – UPDATE – DELETE
See: hfp://dev.mysql.com/doc/refman/5.6/en/sql-‐syntax-‐data-‐manipula>on.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 185 -‐
![Page 186: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/186.jpg)
SELECT SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [PARTITION par>>on_list] [WHERE where_condi>on] [GROUP BY {col_name | expr | posi>on} [ASC | DESC], ... [WITH ROLLUP]]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 186 -‐
[HAVING where_condi>on] [ORDER BY {col_name | expr | posi>on} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_op>ons | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]
![Page 187: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/187.jpg)
JOIN join_table: table_reference [INNER | CROSS] JOIN table_factor [join_condi/on] | table_reference STRAIGHT_JOIN table_factor | table_reference STRAIGHT_JOIN table_factor ON condi/onal_expr | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condi/on | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor where table_factor: tbl_name [PARTITION (par//on_names)] [[AS] alias] [index_hint_list] | table_subquery [AS] alias | ( table_references )
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 187 -‐
![Page 188: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/188.jpg)
UNION
SELECT ... UNION [ALL | DISTINCT] SELECT ... [UNION [ALL | DISTINCT] SELECT ...]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 188 -‐
![Page 189: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/189.jpg)
Subqueries
• A subquery is a query returning a resultset to another query
select * from t1 where t1.a in (select t2.a from t2); • It can be used in select, update, insert, delete • Almost all subqueries can be rewrifen as JOIN clauses
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 189 -‐
![Page 190: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/190.jpg)
Subquery as scalar operand
select (select a from t1); select lower(select city_name from ci>es) from ci>es_lower;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 190 -‐
![Page 191: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/191.jpg)
Subquery as a comparison
select * from t1 where a = (select max(a) from t1);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 191 -‐
![Page 192: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/192.jpg)
Subquery EXISTS and NOT EXISTS
SELECT DISTINCT store_type FROM stores WHERE EXISTS (SELECT * FROM ci>es_stores WHERE ci>es_stores.store_type = stores.store_type); SELECT DISTINCT store_type FROM stores WHERE NOT EXISTS (SELECT * FROM ci>es_stores WHERE ci>es_stores.store_type = stores.store_type);
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 192 -‐
![Page 193: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/193.jpg)
Correlated subquery
• Used to query only the rows of the query who have a result in the subquery
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2 WHERE t2.column2 = t1.column2);
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 193 -‐
![Page 194: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/194.jpg)
Subquery as a pseudo-‐table
select b,sum(a) from (select a,b from t1 where a > 2) as t1a group by b;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 194 -‐
![Page 195: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/195.jpg)
HANDLER • A handler is a par>cular object made to browse a par>cular table using its indexes • It is much faster than a SELECT statement and can be reused • Prefer handler to select for single tables HANDLER tbl_name OPEN [ [AS] alias] HANDLER tbl_name READ index_name { = | <= | >= | < | > } (value1,value2,...) [ WHERE where_condi/on ] [LIMIT ... ] HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST } [ WHERE where_condi/on ] [LIMIT ... ] HANDLER tbl_name READ { FIRST | NEXT } [ WHERE where_condi/on ] [LIMIT ... ] HANDLER tbl_name CLOSE
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 195 -‐
![Page 196: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/196.jpg)
INSERT INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (par//on_name,...)] [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] Or: INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (par//on_name,...)] SET col_name={expr | DEFAULT}, ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 196 -‐
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (par//on_name,...)] [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ]
![Page 197: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/197.jpg)
UPDATE • Single-‐table syntax: UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condi/on] [ORDER BY ...] [LIMIT row_count] • Mul>ple-‐table syntax: UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condi/on]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 197 -‐
![Page 198: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/198.jpg)
DELETE
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [PARTITION (par//on_name,...)] [WHERE where_condi/on] [ORDER BY ...] [LIMIT row_count]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 198 -‐
![Page 199: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/199.jpg)
Language structure
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 199 -‐
![Page 200: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/200.jpg)
String literals
'this is a string' "this is another string" 'this ' 'is' ' another string' SELECT _charset_name'a string' COLLATE other_charset_name SELECT N'na>onal character set string' make quotes: SELECT 'hello','"hello"','hel''lo','\'hello'; SELECT "hello","'hello'","hel""lo","\"hello";
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 200 -‐
![Page 201: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/201.jpg)
Number literals
1.2 -‐3 -‐4.56 +7.89 1.2E3 4.5E-‐6 -‐7.8E-‐9
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 201 -‐
![Page 202: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/202.jpg)
Date and Time literals '2014-‐04-‐23' (any punctua>on delimiter can be used) '20140423' 20140423 Possible formats are: 'YYYY-‐MM-‐DD HH-‐MM-‐SS[.nnnnnn]' 'YYYY-‐MM-‐DDTHH-‐MM-‐SS[.nnnnnn]' 'YYYYMDDHHMMSS[.nnnnnn]' 'YYMMDDHHMMSS[.nnnnnn]' YYYYMDDHHMMSS[.nnnnnn] YYMMDDHHMMSS[.nnnnnn] YYMMDD[.nnnnnn] YYYYMMDD[.nnnnnn]
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 202 -‐
![Page 203: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/203.jpg)
Hexadecimal literals X'val' x'val' 0xval … where val is a combina>on of: 0 1 2 3 4 5 6 7 8 9 A B C D E F or 0 1 2 3 4 5 6 7 8 9 a b c d e f Usage: SELECT 0x2F + 0; à 47 SELECT 0x416374; à 'Act' SELECT HEX('Act'); à '416374'
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 203 -‐
![Page 204: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/204.jpg)
Boolean literals
SELECT TRUE, true, FALSE, false; à 1, 1, 0, 0
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 204 -‐
![Page 205: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/205.jpg)
Bit-‐Field literals CREATE TABLE t (b BIT(8)); INSERT INTO t SET b = b'11111111'; INSERT INTO t SET b = b'1010'; INSERT INTO t SET b = b'0101'; SELECT b+0, BIN(b+0), OCT(b+0), HEX(b+0) FROM t; +------+----------+----------+----------+ | b+0 | BIN(b+0) | OCT(b+0) | HEX(b+0) | +------+----------+----------+----------+ | 255 | 11111111 | 377 | FF | | 10 | 1010 | 12 | A | | 5 | 101 | 5 | 5 | +------+----------+----------+----------+
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 205 -‐
![Page 206: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/206.jpg)
NULL values
NULL \N
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 206 -‐
![Page 207: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/207.jpg)
Schema object names
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 207 -‐
![Page 208: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/208.jpg)
Iden>fier qualifiers
col_name tbl_name.col_name db_name.tbl_name.col_name `my-‐table` OK my-‐table NOK
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 208 -‐
![Page 209: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/209.jpg)
Iden>fier case sensi>vity
table_name <> TABLE_NAME
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 209 -‐
![Page 210: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/210.jpg)
Reserved words
• See: hfp://dev.mysql.com/doc/refman/5.6/en/reserved-‐words.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 210 -‐
![Page 211: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/211.jpg)
User-‐defined variables and dynamic SQL
SET @var_name = expr [,@var_name := expr] SET @pi = 3.1415926535; SELECT 2 * radius * @pi FROM all_radius; Dynamic SQL: SET @col_name = 'col1'; SET @stmt = CONCAT('SELECT ',@col_name,' FROM tab1'); PREPARE stmt from @stmt; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 211 -‐
![Page 212: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/212.jpg)
Expression syntax
• See: hfp://dev.mysql.com/doc/refman/5.6/en/expressions.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 212 -‐
![Page 213: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/213.jpg)
Comment syntax and hints
# Comment 1 -‐-‐ Comment 2 /* Comment 3 */ /* This is comment 4 */ Hints: SELECT /*! STRAIGHT_JOIN */ col1 FROM tab1,table2 WHERE …
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 213 -‐
![Page 214: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/214.jpg)
Globaliza>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 214 -‐
![Page 215: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/215.jpg)
Globaliza>on
• See: hfp://dev.mysql.com/doc/refman/5.6/en/expressions.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 215 -‐
![Page 216: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/216.jpg)
Data types
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 216 -‐
![Page 217: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/217.jpg)
Numeric types
• INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
• DECIMAL, NUMERIC • FLOAT, DOUBLE • BIT
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 217 -‐
![Page 218: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/218.jpg)
Date and >me types
• DATE, DATETIME, TIMESTAMP • TIME • YEAR
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 218 -‐
![Page 219: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/219.jpg)
String types
• CHAR, VARCHAR • BINARY, VARBINARY • BLOB, TEXT • ENUM • SET
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 219 -‐
![Page 220: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/220.jpg)
… and more
• See: hfp://dev.mysql.com/doc/refman/5.6/en/data-‐types.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 220 -‐
![Page 221: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/221.jpg)
Func>ons and operators
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 221 -‐
![Page 222: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/222.jpg)
Func>on and operator reference
• See: hfp://dev.mysql.com/doc/refman/5.6/en/func-‐op-‐summary-‐ref.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 222 -‐
![Page 223: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/223.jpg)
Control flow func>ons • CASE value WHEN compare value THEN result […] ELSE result END
• CASE WHEN condi>on THEN result […] ELSE result END
• IF(expr1,res1,res2) à if expr1 is true then res1 else res2
• IFNULL(expr1,expr2) à if expr1 is null then expr2 else expr1
• NULLIF(expr1,expr2) à NULL if expr1 == expr2 else expr1
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 223 -‐
![Page 224: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/224.jpg)
Full-‐text search
• See: hfp://dev.mysql.com/doc/refman/5.6/en/fulltext-‐search.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 224 -‐
![Page 225: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/225.jpg)
… and more
• hfp://dev.mysql.com/doc/refman/5.6/en/func>ons.html
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 225 -‐
![Page 226: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/226.jpg)
Store InnoDB tables
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 226 -‐
![Page 227: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/227.jpg)
Storing tables
set global innodb_file_per_table=<0|1> create table <table> data directory <absolute path to directory> index directory <absolute path to directory>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 227 -‐
![Page 228: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/228.jpg)
Par>>oning
• Par>>oning is made to split data across mul>ple pseudo-‐tables
• The execu>on plans benefit of that spli`ng • Accessing a par>>on is same as accessing a table but with the data volume of the par>>on
• Mul>ple queries on mul>ple par>>on are not in concurrency if each query is on each par>>on
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 228 -‐
![Page 229: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/229.jpg)
Range par>>oning create table t1 ( id int not null, data1 varchar(100), data2 varchar(50) ) par>>on by range(id) (
par>>on p0 values less than (10), par>>on p1 values less than (20), par>>on p2 values less than (30), par>>on p3 values less than (40), par>>on p4 values less than (50), par>>on p5 values less than (60), par>>on pn values less than maxvalue
);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 229 -‐
![Page 230: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/230.jpg)
List par>>oning (integer values) create table t1 ( id int not null, data1 varchar(100), data2 varchar(50) ) par>>on by list(id) (
par>>on pstart values in (1,2,3,4), par>>on pmiddle values in (11,12,13,15), par>>on pend values in (45,55,57,58)
);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 230 -‐
![Page 231: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/231.jpg)
Range columns par>>oning create table t1( id1 int not null, id2 int not null, data1 varchar(10), data2 varchar(10) ) par>>on by range columns(id1,id2,data1)
par>>on p0 values less than (2,10,'f'), par>>on p1 values less than (3,10,'g'), par>>on p2 values less than (5,100,'az'), par>>on pmax values less than (MAXVALUE,MAXVALUE,MAXVALUE)
);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 231 -‐
![Page 232: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/232.jpg)
List columns par>>oning create table t1( region varchar(100) not null, city varchar(100) not null ) par>>on by list columns(dept)( par>>on pregion01 values in('Paris','Seine-‐et-‐Marne','Yvelines','Essonne','Hauts-‐de-‐Seine','Val-‐d'Oise','Val-‐de-‐Marne',Seine-‐et-‐Oise'), par>>on pregion02 values in('Corse-‐Nord','Corse-‐Sud'), … );
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 232 -‐
![Page 233: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/233.jpg)
[Linear] hash par>>oning
create table products_references( ref_id int not null, product_name varchar(200) not null quan>ty int not null ) par>>on by [linear] hash(ref_id) par>>ons 16;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 233 -‐
![Page 234: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/234.jpg)
Key par>>oning
• Par>>oning depending on a key in the table (primary, unique)
create table key1( id int not null primary key, name varchar(100) ) par>>on by key() par>>ons 10;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 234 -‐
![Page 235: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/235.jpg)
Subpar>>oning • … is a combina>on of a par>>oning by:
– range or list • with a subpar>>oning by:
– hash or key • named composite par>>oning too create table cp( id int not null, ref_id int not null ) par>>on by range(id) subpar>>on by hash(ref_id) ( par>>on p0 values less than (2)(
subpar>>on s0, subpar>>on s1
), par>>on p1 values less than(3)(
subpar>>on s2, subpar>>on s3
) );
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 235 -‐
![Page 236: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/236.jpg)
Distribute data and index of par>>ons or subpar>>ons
• It is possible to distribute par>>ons and subpar>>ons across different storages
par>>on p1 values less than(2) data directory = '/data/p1' index directory = '/index/p1' (same syntax for subpar>>oning)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 236 -‐
![Page 237: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/237.jpg)
Managing par>>ons
• Managing par>>ons is essen>al to: – add par>>ons – reorganize par>>ons – drop par>>ons – archive old data – exchange par>>ons
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 237 -‐
![Page 238: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/238.jpg)
Add par>>ons
alter table <table> add par>>on( par>>on <par>>on> values less than(<value>));
alter table <table> add par>>on( par>>on <par>>on> values in (<value list>));
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 238 -‐
![Page 239: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/239.jpg)
Reorganize par>>ons
alter table <table> reorganize par>>on <par>>on list> into (<par>>on defini>on>);
alter table t1 reorganize par>>on p0,p1,p2,p3 into( par>>on n0 values less than (1000), par>>on n1 values less than (MAXVALUE) );
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 239 -‐
![Page 240: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/240.jpg)
Reorganize par>>ons by key or hash
• To reorganize these kind of par>>ons, use the keyword coalesce
• Coalescing par>>ons corresponds to change the number of par>>ons
alter table <table> coalesce par>>on <number of par>>ons>;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 240 -‐
![Page 241: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/241.jpg)
Exchanging par>>ons • Exchanging a par>>on with a table is used to:
– populate a new par>>on with table data – archive a par>>on – make a rota>on between the par>>ons
• The table needs to have strictly the same structure as the par>>oned table
• The source table may not have foreign keys and may not be referenced by another table
• The source table must not be par>>oned
alter table <table_p> exchange par>>on <p> with table <table>;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 241 -‐
![Page 242: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/242.jpg)
Maintaining par>>ons (not subpar>>ons)
• alter table <table> rebuild par>>on <par>>on list>;
• alter table <table> op>mize par>>on <par>>on list>;
• alter table <table> analyze par>>on <par>>on list>;
• alter table <table> repair par>>on <par>>on list>;
• alter table <table> check par>>on <par>>on list>;
• alter table <table> truncate par>>on <par>>on list>;
• alter table <table> truncate par>>on all;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 242 -‐
![Page 243: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/243.jpg)
Informa>ons for the par>>ons
• show create table <table>\G • explain par>>ons select * from <table>\G
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 243 -‐
![Page 244: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/244.jpg)
Par>>on pruning
• Pruning par>>on is the ac>on of using only the par>>ons containing data when selec>ng them without browse the other par>>ons
• It can be done by using the good column in the WHERE clause with a list of values or a range (BETWEEN)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 244 -‐
![Page 245: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/245.jpg)
Par>>on selec>on
select * from <table> par>>on(<par>>on list>)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 245 -‐
![Page 246: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/246.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 246 -‐
![Page 247: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/247.jpg)
MySQL Compound-‐Statement Procedural language for MySQL
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 247 -‐
![Page 248: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/248.jpg)
Procedures and func>ons CREATE [DEFINER = { user | CURRENT_USER }] PROCEDURE sp_name ([proc_parameter[,...]]) [characteris/c ...] rou/ne_body CREATE [DEFINER = { user | CURRENT_USER }] FUNCTION sp_name ([func_parameter[,...]]) RETURNS type [characteris/c ...] rou/ne_body proc_parameter: [ IN | OUT | INOUT ] param_name type func_parameter: param_name type
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 248 -‐
type: Any valid MySQL data type characteris/c: COMMENT 'string' | LANGUAGE SQL | [NOT] DETERMINISTIC | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } | SQL SECURITY { DEFINER | INVOKER } rou/ne_body: Valid SQL rou/ne statement
![Page 249: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/249.jpg)
Procedure example
CREATE PROCEDURE >tle_count() BEGIN SELECT 'Number of >tles:', COUNT(*) FROM >tles; END;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 249 -‐
![Page 250: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/250.jpg)
Func>on example
CREATE FUNCTION >tle_count() RETURNS int BEGIN DECLARE count_>tles int; SELECT COUNT(*) INTO count_>tles FROM >tles; RETURN count_>tles; END;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 250 -‐
![Page 251: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/251.jpg)
Blocks • A block is defined as: BEGIN … END • Blocks can be nested: BEGIN
BEGIN END
END • Blocks can be labeled: my_label: BEGIN END my_label
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 251 -‐
![Page 252: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/252.jpg)
Statement Label [begin_label:] BEGIN [statement_list] END [end_label] [begin_label:] LOOP
statement_list END LOOP [end_label] [begin_label:] REPEAT
statement_list UNTIL search_condi/on END REPEAT [end_label] [begin_label:] WHILE search_condi/on DO
statement_list END WHILE [end_label]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 252 -‐
![Page 253: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/253.jpg)
DECLARE • Used to declare variables inside a procedure or a func>on
• For each variable to declare, use the keyword DECLARE
CREATE PROCEDURE sp1() BEGIN
DECLARE a int; DECLARE b varchar(100);
[…] END;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 253 -‐
![Page 254: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/254.jpg)
Flow control statements
• Flow can be controled with: – CASE – IF – ITERATE – LEAVE – LOOP – REPEAT – RETURN – WHILE
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 254 -‐
![Page 255: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/255.jpg)
CASE CASE case_value
WHEN when_value THEN statement_list [WHEN when_value THEN statement_list] ... [ELSE statement_list]
END CASE Or: CASE
WHEN search_condi/on THEN statement_list [WHEN search_condi/on THEN statement_list] ... [ELSE statement_list]
END CASE
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 255 -‐
![Page 256: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/256.jpg)
IF
IF search_condi/on THEN statement_list [ELSEIF search_condi/on THEN statement_list] ... [ELSE statement_list]
END IF
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 256 -‐
![Page 257: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/257.jpg)
ITERATE and LEAVE
• ITERATE label • LEAVE label
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 257 -‐
![Page 258: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/258.jpg)
LOOP
[begin_label:] LOOP statement_list
END LOOP [end_label]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 258 -‐
![Page 259: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/259.jpg)
REPEAT
[begin_label:] REPEAT statement_list
UNTIL search_condi/on END REPEAT [end_label]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 259 -‐
![Page 260: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/260.jpg)
WHILE
[begin_label:] WHILE search_condi/on DO
statement_list END WHILE [end_label]
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 260 -‐
![Page 261: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/261.jpg)
CURSOR • Cursors have to be defined aner all declared variables and before any handler CREATE PROCEDURE curdemo() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE a CHAR(16); DECLARE b, c INT; DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; DECLARE cur2 CURSOR FOR SELECT i FROM test.t2; OPEN cur1; OPEN cur2; read_loop: LOOP FETCH cur1 INTO a, b; FETCH cur2 INTO c; IF done THEN LEAVE read_loop; END IF; IF b < c THEN INSERT INTO test.t3 VALUES (a,b); ELSE INSERT INTO test.t3 VALUES (a,c); END IF; END LOOP; CLOSE cur1; CLOSE cur2; END;
Copyright 2014 – Oracle Corp Reproduc>on interdite sans accord préalable -‐ 261 -‐
![Page 262: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/262.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 262 -‐
![Page 263: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/263.jpg)
What is Javascript?
• Javascript was first Livescript • Renamed Javascript because quite similar as Java
• Light typed • Object-‐Oriented but without the complexity of Java
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 263 -‐
![Page 264: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/264.jpg)
Where Javascript needs to be placed?
• Javascript is found in markups <script></script> • It can be coded directly in these markups • … or placed in a .js file • In that last case the syntax is <script src="myscript.js"></script>
• … where myscript.js is your script • Prefer to place the scripts in the <head> part of the HTML page
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 264 -‐
![Page 265: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/265.jpg)
Javascript and HTML objects events
• Javascript can be found inside objects <input type="text" name="toto" id="toto" onkeyup="{myFunc>onForToto(this);}">
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 265 -‐
![Page 266: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/266.jpg)
Javascript and DOM manipula>ons
• Javascript manipulates the DOM • Every object of the DOM can be modified • Javascript is made to create dynamically new objects
• … or to destroy objects
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 266 -‐
![Page 267: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/267.jpg)
Literals
• Number literals: 10.5 100e10 -‐15 • String literals: "This is a string" 'This is another string'
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 267 -‐
![Page 268: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/268.jpg)
Variables • They are defined with the keyword var • Defining variables is mandatory
var x; var y; x = 10; y = "Test"; • Variables have to be wrifen as in Java, beginning with lowercase and uppercase for each word from the second
thisIsAVariable = "with a value";
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 268 -‐
![Page 269: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/269.jpg)
Data types
• Javascript uses dynamic data types var a; // Undefined type var a = 5; // Type is now numeric. Same variable var a = "Test"; // Same variable, but now string • Main types are: – Number – String – Boolean – Object
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 269 -‐
![Page 270: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/270.jpg)
Data types as objects
var a = new Number; var b = new String; var c = new Boolean; var d = new Date();
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 270 -‐
![Page 271: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/271.jpg)
Date object
• new Date() // current date and >me • new Date(milliseconds) //milliseconds since 1970/01/01
• new Date(dateString) • new Date(year, month, day, hours, minutes, seconds, milliseconds)
• … and many methods: see hfp://www.w3schools.com/jsref/jsref_obj_date.asp
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 271 -‐
![Page 272: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/272.jpg)
Arrays
• Arrays are objects var a = new Array(); a[0] = new Array(); // Mul>dimensional array a[0][0] = "value"; a[1] = new Array(); a[1][0] = "another value"; var grades = ["Algorithmic","Mathema>c","Database"]; var differentElements = ["1st element",2,true]; Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 272 -‐
![Page 273: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/273.jpg)
Number of elements in an array
var l = a.length;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 273 -‐
![Page 274: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/274.jpg)
Posi>on of an element in an array
var p = grades.indexOf("Database");
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 274 -‐
![Page 275: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/275.jpg)
Math object
• This objects contains constants and methods for mathema>cal opera>ons
• Constants: E, LN2, LN10, LOG2E, LOG10E, PI, SQRT1_2, SQRT2 • Methods: abs(x), acos(x), asin(x), atan(x), atan2(y,x), ceil(x), cos(x), exp(x), floor(x), log(x), max(x,y,z,…,n), min(x,y,z,…,n), pow(x,y), random(), round(x), sin(x), sqrt(x), tan(x)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 275 -‐
![Page 276: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/276.jpg)
Operators • They are as in Java and other common languages • Arithme>cs: + -‐ * / % ++ -‐-‐ • Assignment: = += -‐= *= /= %= • Strings: a = "this is"; b = " a concatenated string"; c = a + b;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 276 -‐
![Page 277: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/277.jpg)
Comparison and logical operators
• Comparison operators: == === != !== > < >= <= • Logical operators: && || !
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 277 -‐
![Page 278: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/278.jpg)
Comments
• They are as in Java // This is a comment /* This is another one comment */
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 278 -‐
![Page 279: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/279.jpg)
Case sensi>vity
• Javascript is case sensi>ve
var thisVariable = "is not"; var thisvariable;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 279 -‐
![Page 280: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/280.jpg)
Instruc>on separator
• The instruc>on separator is the semicolon alert("Next step"); s++;
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 280 -‐
![Page 281: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/281.jpg)
Code block
• A code block is defined by curly brackets {
// This is a code block; a-‐-‐;
} • Code blocks are useful for control flow blocks
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 281 -‐
![Page 282: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/282.jpg)
Condi>ons statements if(<condi>on>){
// instruc>ons } if(<condi>on>){
// instruc>ons }else{
// instruc>ons } if(<condi>on>){
// instruc>ons }else if(<condi>on>){
// instruc>ons }else{
//instruc>ons }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 282 -‐
![Page 283: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/283.jpg)
Switch switch(<expression>){ case n:
// instruc>ons [break;]
case m: // instruc>ons [break;]
default: // instruc>ons
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 283 -‐
![Page 284: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/284.jpg)
For loop
for(<variable> = <value>;<ending condi>on>;<opera>on on the variable>){
// instruc>ons }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 284 -‐
![Page 285: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/285.jpg)
While loop
while(<condi>on>){ // instruc>ons
} do{
// instruc>ons while(<condi>on>);
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 285 -‐
![Page 286: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/286.jpg)
Break and con>nue
• break is used to leave the loop • con=nue is used to go to the next itera>on of the loop
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 286 -‐
![Page 287: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/287.jpg)
Errors management
try{ // instruc>ons throw "<message>";
}catch(<error>){ // instruc>ons // <error>.message returns the error message // in case of throw, <error> is the message
} Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 287 -‐
![Page 288: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/288.jpg)
Regular expressions
• As in any other languages like awk and sed /<pafern>/<modifiers>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 288 -‐
![Page 289: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/289.jpg)
Strict mode • To protect the code, use strict mode: "use strict"; • This disable the ability to create accidentally a global variable inside a local code
• This ensure the strict applica>on of all the rules of the objects: – Impossible to delete a undeletable property – Impossible to write a read-‐only property – with is not allowed – …
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 289 -‐
![Page 290: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/290.jpg)
Javascript events
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 290 -‐
![Page 291: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/291.jpg)
Events
• The events are linked to the objects present in a page
• An event is a kind of trigger on a specific ac>on on a par>cular object
• Javascript handle a huge number of events • This is the main usage of Javascript
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 291 -‐
![Page 292: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/292.jpg)
Main events
• onchange: When the state of an HTML element changes
• onclick: When the user clicks an HTML element • onmouseover: When the mouse moves over an HTML element
• onmouseout: When the mouse moves away from an HTML element
• onkeydown: When the user pushes a keyboard key • onload: When the browser has finished loading a page
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 292 -‐
![Page 293: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/293.jpg)
Complete list of events
• See: hfp://www.w3schools.com/jsref/dom_obj_event.asp
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 293 -‐
![Page 294: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/294.jpg)
Some examples
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 294 -‐
![Page 295: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/295.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 295 -‐
![Page 296: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/296.jpg)
What is Ajax?
• Ajax is the acronym for Asynchronous Javascript And XML
• Ajax is a toolset to create fast and dynamically WEB pages
• Ajax works with the events • Ajax sends data to the server • … and receives data, process it • … to update the page content
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 296 -‐
![Page 297: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/297.jpg)
Ajax objects
• XMLHfpRequest: object to exchange data asynchronously with the server
• Javascript/DOM: interac>on with the page • CSS: Style the data • XML: transfer data between the client and the server
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 297 -‐
![Page 298: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/298.jpg)
XMLHfpRequest
Var xmlhfp = new XMLHfpRequest(); // Defines an Ajax component xmlhfp.onreadystatechange = func>on(){ … } // An unnamed func>on containing what to do when the result is returned by the serve xmlhfp.open("[GET|POST]","<URL>",[true|false]); // Open a link with the server. The URL contains the file who will be processed server-‐side xmlhfp.send(); // Sends the request Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 298 -‐
![Page 299: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/299.jpg)
responseText vs responseXML
• Depending on the complexity of the returned data, use: – responseText for simple data – responseXML for complex data
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 299 -‐
![Page 300: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/300.jpg)
responseXML
• Browsing the response can be like this: var xml = xmlhfp.responseXML; var i = 0; while(xml.getElementByTagName(<tag>)[i]{
// Process the element i++;
}
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 300 -‐
![Page 301: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/301.jpg)
Ajax events
Source hfp://w3schools.com:
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 301 -‐
Property Descrip_on
onreadystatechange Stores a func>on (or the name of a func>on) to be called automa>cally each >me the readyState property changes
readystate Holds the status of the XMLHfpRequest. Changes from 0 to 4: 0: request not ini>alized 1: server connec>on established 2: request received 3: processing request 4: request finished and response is ready
status 200: "OK" 404: Page not found
![Page 302: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/302.jpg)
PHP file returning data to Ajax
<?php $res = ""; $res = process($_GET); echo $res; // Echoing the variable is necessary to return results to the Ajax caller ?>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 302 -‐
![Page 303: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/303.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 303 -‐
![Page 304: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/304.jpg)
What is jQuery?
• jQuery is a Javascript library made to simplify Javascript wri>ng
• It simplifies Ajax wri>ng too • Easy to learn • jQuery runs exactly same in all major browsers, including… IE 6!!
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 304 -‐
![Page 305: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/305.jpg)
Loading jQuery
• jQuery library needs to be loaded before used <script src="jquery-‐1.11.0.min.js"></script> • It's befer to download the file in the same directory as all the other Javascript files of the applica>on
• jQuery can be loaded from the WEB: <script src="hfp://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 305 -‐
![Page 306: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/306.jpg)
jQuery syntax
$(selector).ac>on() • … where: • $ defines jQuery • (selector) is HTML element(s) (query) • ac>on() is the ac>on to be performed on the element(s)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 306 -‐
![Page 307: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/307.jpg)
jQuery selectors
$("p") // All <p> elements of a page $("#id") // Corresponds to the <element id="id"> $("p.classname") // All <p class="classname"> See: hfp://www.w3schools.com/jquery/jquery_ref_selectors.asp … for the selectors reference
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 307 -‐
![Page 308: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/308.jpg)
Trigger events
$(<element>).ac>on(func>on(){ // Code of the func>on
}); • Normally all the ac>ons need to be encapsulated into:
$(document).ready()(func>on(){ // All the methods
});
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 308 -‐
![Page 309: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/309.jpg)
jQuery and Ajax
• jQuery simplifies Ajax • Three methods are necessary to manage all Ajax needs: – $(selector).load(URL,data,callback) – $(selector).get(URL,data,callback) – $(selector).post(URL,data,callback)
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 309 -‐
![Page 310: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/310.jpg)
Some examples
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 310 -‐
![Page 311: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/311.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea_ng a plugin – Crea>ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 311 -‐
![Page 312: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/312.jpg)
Wordpress
• Wordpress is a Content Management System (CMS)
• Wordpress is a framework • Wordpress is expandable through themes, plugins and widgets
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 312 -‐
![Page 313: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/313.jpg)
Wordpress structure
• wp-‐content – plugins – themes
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 313 -‐
![Page 314: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/314.jpg)
Plugin structure
• plugin-‐name (directory) – plugin-‐name.php (mandatory file) – your own paths for all your program files
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 314 -‐
![Page 315: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/315.jpg)
Plugin main file header /*
Plugin Name: Module d'Administration des Académiciens
Plugin URI: http://www.champavert-consulting.fr/
Description: Mise à jour des données des académiciens présents dans la base acadmed
Version: 26.0
Author: Alexandra Champavert
Author URI: http://www.champavert-consulting.fr/ */
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 315 -‐
![Page 316: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/316.jpg)
Shortcodes
• A shortcode is a code that can be inserted into a WP page: [shortcode-‐name]
• The WP PHP primi>ve corresponding to that shortcode is:
add_shortcode("shortcode-‐name",array(<php object>,"<php method of the object>")) ex: add_shortcode("maj_>tres",array(&$maj_academiciens,"majTitres"); Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 316 -‐
![Page 317: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/317.jpg)
Class for WP for shortcodes Class MAJAcademiciens { […] func>on majTitres(){ $this-‐>content = null; // The code returning a string that can be displayed in the page containing the shortcode return $this-‐>content; } […] }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 317 -‐
![Page 318: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/318.jpg)
Wordpress and Ajax • Needs to enqueue specific scripts inside WP: wp_enqueue_script("alias_js_script",<js file>,array("jquery"),true); • Needs to implement a standard ac>on: add_ac>on("wp_ajax_nopriv_alias_method",array(<class variable>,"<method>")); • … and a connected ac>on: add_ac>on("wp_ajax_alias",array(<class variable>,"<method>")); • … and finally the localiza>on of the script: wp_localize_script("alias","class name",array( "ajaxurl" => admin_url("admin-‐ajax.php"), "ac>on" => "alias_method"));
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 318 -‐
![Page 319: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/319.jpg)
Wordpress and Ajax example wp_enqueue_script("maj_academiciens_cp_js_script", plugin_dir_url(__FILE__)."js/cp_villes.js", array("jquery"),true); add_ac>on("wp_ajax_nopriv_maj_academiciens_cp_cpvsv",array(&$maj_academiciens,"codesPostauxVillesSearchVilles")); add_ac>on("wp_ajax_maj_academiciens_cp_cpvsv",array(&$maj_academiciens,"codesPostauxVillesSearchVilles")); wp_localize_script("maj_academiciens_cp_js_script", "MAJAcademiciens", array( "ajaxurl" => admin_url("admin-‐ajax.php"), "ac>on" => "maj_academiciens_cp_cpvsv" ) );
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 319 -‐
![Page 320: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/320.jpg)
Ajax inside the class for WP class MAJAcademiciens { func>on codesPostauxVillesSearchVilles(){ $error_code = 0; […] header( "Content-‐Type: applica>on/json" ); echo json_encode( array( "error" => $error_code, "cpt_villes" => $cpt, "liste_villes" => $liste, "liste_codes_postaux" => $liste_cp ) ); exit; }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 320 -‐
![Page 321: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/321.jpg)
jQuery for WP for Ajax jQuery("#search_villes").keyup(func>on(){ jQuery.ajax({ type: "POST", url: MAJAcademiciens.ajaxurl, datatype: "json", async: true, cache: false, >meout: 10000, data: { ac>on: "maj_academiciens_cp_cpvsv", search_villes: jQuery("#search_villes:text").val() }, success: func>on(data){ if(data.error == 0){ if(data.cpt_villes > 30){ // jQuery("p#show_liste_villes").html("<select name=\"liste_villes\" id=\"liste_villes\"></select>"); jQuery("#liste_villes").empty(); jQuery("select#liste_villes").afr("size",1); jQuery("select#liste_villes").css("display","none"); jQuery("p#show_villes").css("display","none"); jQuery("p#show_liste_villes").css("display","none"); jQuery("p#count_villes").css("display","inline-‐block"); jQuery("p#count_villes").html(data.cpt_villes + " communes"); }else if(data.cpt_villes == 1){ jQuery("p#count_villes").css("display","none"); jQuery("p#show_villes").css("display","inline"); for(var i = 0; i < data.liste_villes.length;i++){ jQuery("#h_liste_villes").val(data.liste_villes[i].villes_id); jQuery("#s_liste_villes").html(data.liste_villes[i].ville + " (" + data.liste_villes[i].no_dept + ")"); } jQuery("#show_search_villes").css("display","none"); jQuery("#liste_villes").css("display","none"); jQuery("#s_liste_villes").css("display","inline"); jQuery("#search_codes_postaux").val(""); jQuery("#liste_codes_postaux").empty(); jQuery("#show_submit_insert_cp_ville").css("display","none"); jQuery("#show_codes_postaux").css("display","none"); jQuery("select#liste_codes_postaux").afr("size",1); jQuery("select#liste_codes_postaux").css("display","none"); jQuery("p#count_codes_postaux").css("display","none"); jQuery("p#linked_codes_postaux").css("display","inline"); jQuery("#liste_villes").css("display","none");
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 321 -‐
jQuery("#show_search_codes_postaux").css("display","inline"); jQuery("select#liste_villes").afr("size",1); liste_cp = ""; if(data.liste_codes_postaux.length){ for(var i = 0; i < data.liste_codes_postaux.length;i++){ liste_cp = liste_cp + "<div style=\"width: 200px\; posi>on: rela>ve; display: inline-‐block;\">" + data.liste_codes_postaux[i].code_postal + "</div>"; } } jQuery("p#linked_codes_postaux").html(liste_cp); }else if(data.cpt_villes == 0){ jQuery("#liste_villes").empty(); jQuery("p#count_villes").css("display","inline"); jQuery("p#show_villes").css("display","none"); jQuery("p#show_liste_villes").css("display","inline"); jQuery("select#liste_villes").css("display","inline"); jQuery("select#liste_villes").afr("size",data.liste_villes.length); }else{ jQuery("#liste_villes").empty(); jQuery("#liste_villes").css("display","inline"); jQuery("p#count_villes").css("display","none"); jQuery("p#show_villes").css("display","inline"); jQuery("p#show_liste_villes").css("display","inline"); jQuery("select#liste_villes").css("display","inline"); jQuery("select#liste_villes").afr("size",data.liste_villes.length); for(var i = 0; i < data.liste_villes.length;i++){ jQuery("select#liste_villes").append("<op>on value=\"" + data.liste_villes[i].villes_id +"\">" + data.liste_villes[i].ville + " (" + data.liste_villes[i].no_dept + ")</op>on>"); } } } else{ jQuery("p#count_villes").html("Liste trop longue"); } }, error: func>on(XMLHfpRequest,textStatus,errorThrown){ jQuery("p#count_villes").html("Aucune commune"); } }); });
![Page 322: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/322.jpg)
Ajax, details jQuery("#search_villes").keyup(func>on(){ jQuery.ajax({ type: "POST", url: MAJAcademiciens.ajaxurl, datatype: "json", async: true, cache: false, >meout: 10000, data: { ac>on: "maj_academiciens_cp_cpvsv", search_villes: jQuery("#search_villes:text").val() }, success: func>on(data){ […] for(var i = 0; i < data.liste_villes.length;i++){ jQuery("select#liste_villes").append("<op>on value=\"" + data.liste_villes[i].villes_id +"\">" + data.liste_villes[i].ville + " (" + data.liste_villes[i].no_dept + ")</op>on>"); } […] }, error: func>on(XMLHfpRequest,textStatus,errorThrown){ jQuery("#" + count_ville + n).html("Aucune commune"); } }); Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 322 -‐
![Page 323: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/323.jpg)
Using the WP database func>on getCompteVilles($debut){ global $wpdb; $stmt = $this-‐>sqlInst["get-‐compte-‐villes"]; $stmt = str_replace("[prefix]", $this-‐>prefix_views, $stmt); $sql = $wpdb-‐>prepare($stmt,$debut."%"); // echo $sql; return($wpdb-‐>get_results($sql)); } The query handled by this func>on is: select count(1) as compte_villes from [prefix]v_villes where ville_descrip>on like %s /*+NO_PARSE */
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 323 -‐
![Page 324: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/324.jpg)
How it appears in the administra>on panel?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 324 -‐
![Page 325: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/325.jpg)
How can it be called in a page (example)?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 325 -‐
![Page 326: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/326.jpg)
What is generated by the func>on inside the plugin?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 326 -‐
![Page 327: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/327.jpg)
Session plan • Introduc>on to databases
– Databases and management system – Normal forms
• Fundamentals – HTML & CSS – PHP
• Connec>ng from PHP to Databases – PDO
• MySQL – SQL language – Compound-‐Statement (Procedural Language)
• Advanced WEB development – Javascript – Ajax – jQuery
• Wordpress – Crea>ng a plugin – Crea_ng a widget
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 327 -‐
![Page 328: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/328.jpg)
Widget structure
• Same as plugin structure
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 328 -‐
![Page 329: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/329.jpg)
Widget main file header
• Same as plugin
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 329 -‐
![Page 330: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/330.jpg)
Class defini>on class LastPublica>onsWidget extends WP_Widget { var $arrayDataAchAnmPublica>ons; var $arrayRes; func>on LastPublica>onsWidget() { $widget_ops = array('classname' => 'LastPublica>onsWidget', 'descrip>on' => 'Présente les dernières publica>ons' ); $this-‐>WP_Widget('LastPublica>onsWidget', 'Dernières Publica>ons', $widget_ops); }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 330 -‐
![Page 331: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/331.jpg)
Menu entry in the administra>on panel
func_on form($instance) { $instance = wp_parse_args( (array) $instance, array( '>tle' => '' ) ); $>tle = $instance['>tle']; ?> <p><label for="<?php echo $this-‐>get_field_id('>tle'); ?>">Title: <input class="widefat" id="<?php echo $this-‐>get_field_id('>tle'); ?>" name="<?php echo $this-‐>get_field_name('>tle'); ?>" type="text" value="<?php echo afribute_escape($>tle); ?>" /></label></p> <p> <label for="<?php echo $this-‐>get_field_id('numberposts'); ?>">Number of posts:</label> <select id="<?php echo $this-‐>get_field_id('numberposts'); ?>" name="<?php echo $this-‐>get_field_name('numberposts'); ?>"> <?php for ($i=1;$i<=20;$i++) { echo '<op_on value="'.$i.'"'; if ($i==$instance['numberposts']) echo ' selected="selected"'; echo '>'.$i.'</op_on>'; } ?> </select> </p> <?php }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 331 -‐
![Page 332: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/332.jpg)
Upda>ng the data from admin panel
func_on update($new_instance, $old_instance) { $instance = $old_instance; $instance['>tle'] = $new_instance['>tle']; $instance['numberposts'] = $new_instance['numberposts']; return $instance; }
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 332 -‐
![Page 333: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/333.jpg)
The widget
func>on widget($args, $instance){ extract($args, EXTR_SKIP); […] if (!empty($>tle)){ echo $>tle; } // All the data to display via echo or prin� } Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 333 -‐
![Page 334: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/334.jpg)
Register the widget
add_ac>on( 'widgets_init', create_func>on('', 'return register_widget("LastPublica>onsWidget");'));
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 334 -‐
![Page 335: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/335.jpg)
How it appears in the administra>on panel?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 335 -‐
![Page 336: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/336.jpg)
How can it be setup?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 336 -‐
![Page 337: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/337.jpg)
What is generated by the widget?
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 337 -‐
![Page 338: EPITA Advanced&WEB&Development& and Advanced&Database](https://reader031.vdocument.in/reader031/viewer/2022012916/61c6676862a31d6dd65b2d35/html5/thumbnails/338.jpg)
And now…
THE PROJECT!
Good luck!
Copyright 2014 – Alexandra Champavert Reproduc>on interdite sans accord préalable -‐ 338 -‐