Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

31
Programmazione Web PHP e MySQL 1

Transcript of Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

Page 1: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

Programmazione WebPHP e MySQL

1

Page 2: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

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.

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.

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.

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.

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.

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.

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.

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.

2012/2013 10

Inserimento di un nuovo post

Page 11: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

2012/2013 11

L’archivio del blog

Page 12: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

2012/2013 12

Il foglio di stile

Page 13: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

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.

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.

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.

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.

2012/2013 17

Blog: registrazione di un post

Page 18: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

2012/2013 18

Blog: registrazione di un post

Page 19: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

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.

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.

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.

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.

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.

2012/2013 24Programmazione Web - PHP e MySQL

Esempio

Page 25: Programmazione Web PHP e MySQL 1. 2Programmazione Web - PHP e MySQL Esempio: un blog.

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.

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.

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.

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.

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.

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.

2012/2013 31Programmazione Web - PHP e MySQL

Funzioni ausiliarie (III)Lettura dal database del numero di post: