programmazione web php e mysql 1. 2programmazione web - php e mysql esempio: un blog
TRANSCRIPT
![Page 1: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/1.jpg)
Programmazione WebPHP e MySQL
1
![Page 2: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/2.jpg)
2012/2013 2Programmazione Web - PHP e MySQL
Esempio: un blog
![Page 3: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/3.jpg)
2012/2013 3Programmazione Web - PHP e MySQL
Il blog: inserimento di un post
![Page 4: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/4.jpg)
2012/2013 4Programmazione Web - PHP e MySQL
Il blog: inserimento corretto
![Page 5: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/5.jpg)
2012/2013 5Programmazione Web - PHP e MySQL
Il blog: inserimento non consentito
![Page 6: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/6.jpg)
2012/2013 6Programmazione Web - PHP e MySQL
Il blog: archivio dei post
![Page 7: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/7.jpg)
2012/2013 7Programmazione Web - PHP e MySQL
Blog: file di memorizzazione dei post
![Page 8: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/8.jpg)
2012/2013 8Programmazione Web - PHP e MySQL
Lo script index.php
![Page 9: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/9.jpg)
2012/2013 9Programmazione Web - PHP e MySQL
Lo script di configurazione
![Page 10: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/10.jpg)
2012/2013 10
Inserimento di un nuovo post
![Page 11: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/11.jpg)
2012/2013 11
L’archivio del blog
![Page 12: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/12.jpg)
2012/2013 12
Il foglio di stile
![Page 13: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/13.jpg)
2012/2013 13Programmazione Web - PHP e MySQL
Lettura e scrittura su file (I)int fopen(string nomefile, string modalita): restituisce il puntatore al file o FALSE altrimenti; nomefile
identifica il nome di un file in locale o un URI di risorsa remota, modalita assume i soliti valori r, r+, w, w+, a, a+
bool fclose(int fp): chiude il file il cui puntatore è fp
bool is_dir(int fp), bool is_file(int fp), bool is_link(int fp), bool is_readable(int fp), bool is_writable(int fp):
verificano se il file è una directory, un file normale, un link, è in lettura o in scrittura
string fgetc(int fp): restituisce un carattere letto dal file, aperto in lettura
![Page 14: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/14.jpg)
2012/2013 14Programmazione Web - PHP e MySQL
Lettura e scrittura su file (II)
string fread(int fp, int length): restituisce fino a length byte letti dal file, aperto in lettura
bool feof(int fp): restituisce TRUE se siamo alla fine del file il cui puntatore è fp
int fwrite(int fp, string s, int length): scrive s sul file puntato da fp fino ad un massimo di length byte (opzionale); il file deve essere aperto in scrittura e lafunzione restituisce il numero di byte scritti
bool fflush(int fp): forza la scrittura del contenuto bufferizzato
![Page 15: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/15.jpg)
2012/2013 15Programmazione Web - PHP e MySQL
Lettura e scrittura su file (III)
array file(string nomefile): legge il file il cui nome è nomefile e mette ogni riga come
elemento dell’array restituito
string file_get_contents(string nomefile): è simile a file, ma restituisce il risultato sotto forma di stringa
per ulteriori elaborazioni
int readfile(string nomefile): legge il file e lo emette sullo standard output, restituendo il
numero di byte o FALSE in caso di errore
![Page 16: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/16.jpg)
2012/2013 16Programmazione Web - PHP e MySQL
Blog: autenticazione
![Page 17: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/17.jpg)
2012/2013 17
Blog: registrazione di un post
![Page 18: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/18.jpg)
2012/2013 18
Blog: registrazione di un post
![Page 19: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/19.jpg)
2012/2013 19Programmazione Web - PHP e MySQL
Il costrutto or
La funzione die() nell’esempio precedente blocca l’esecuzione dello script emettendo un messaggio di errore
È stata usata insieme al costrutto or
dove:la funzione f1() restituisce un valore specifico (di qualsiasi tipo)
oppure FALSE in caso di fallimentose f1() fallisce il controllo è passato all’istruzione che segue l’or,
che invece viene ignorata qualora l’esecuzione di f1() abbia successo
$risultato = f1() or istruzione;
![Page 20: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/20.jpg)
2012/2013 20Programmazione Web - PHP e MySQL
Blog: lettura dei post dal file
![Page 21: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/21.jpg)
2012/2013
Interazione tra PHP e MySQL
21Programmazione Web - PHP e MySQL
Browser Server
File applicazioni
Interprete PHP
MySQL
Relazioni database
richiesta “file.php”
“file.php”
“file.php”
query SQL
tuple
tuple
(X)HTML(X)HTML
1
2
3
4
5
678
![Page 22: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/22.jpg)
2012/2013 22Programmazione Web - PHP e MySQL
Script SQL
![Page 23: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/23.jpg)
2012/2013 23Programmazione Web - PHP e MySQL
Connessione a MySQL
resource mysql_connect(string hostname, string username, string password):
apre una connessione al server del database hostname, con utente username e password password
bool mysql_select_db(string nomedb): seleziona il database nomedb
string mysql_error(): messaggio di errore dell’ultima funzione mysql chiamata
bool mysql_close(resource db): chiude la connessione al database
![Page 24: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/24.jpg)
2012/2013 24Programmazione Web - PHP e MySQL
Esempio
![Page 25: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/25.jpg)
2012/2013 25Programmazione Web - PHP e MySQL
Esecuzione di codice SQL
resource mysql_query(string query): invia una query SQL al database a cui si è attualmente connessi
![Page 26: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/26.jpg)
2012/2013
26
Programmazione Web - PHP e MySQL
Trasferimento del risultato della queryint mysql_num_rows(resource res):
numero di tuple del risultato della query relativa a resarray mysql_fetch_row(resource res):
trasforma la riga corrente del risultato in un array non associativo (gli indici corrispondono all’ordine degli attributi selezionati nella query)
$riga
0
1
2
3
1
2011-05-12 09:29:0
Questo è il mio primo post
Che emozione!
Indicizzazione numerica
![Page 27: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/27.jpg)
2012/2013
27
Programmazione Web - PHP e MySQL
Trasferimento del risultato della query
array mysql_fetch_array(resource res): trasforma la riga corrente del risultato in un array associativo (i
nomi degli attributi della query diventano i nomi degli elementi dell’array)
$riga
id
data
titolo
testo
1
2011-05-12 09:29:0
Questo è il mio primo post
Che emozione!
Indicizzazione associativa
![Page 28: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/28.jpg)
2012/2013 28Programmazione Web - PHP e MySQL
Modifica del database
int mysql_affected_rows(): restituisce il numero di tuple coinvolte nella query
appena eseguita
![Page 29: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/29.jpg)
2012/2013 29Programmazione Web - PHP e MySQL
Funzioni ausiliarie (I)Autenticazione dell’utente:
![Page 30: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/30.jpg)
2012/2013 30Programmazione Web - PHP e MySQL
Funzioni ausiliarie (II)
Lettura dei post dal database del blog:
![Page 31: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog](https://reader036.vdocuments.pub/reader036/viewer/2022062303/5542eb76497959361e8e0231/html5/thumbnails/31.jpg)
2012/2013 31Programmazione Web - PHP e MySQL
Funzioni ausiliarie (III)Lettura dal database del numero di post: