Preview only show first 10 pages with watermark. For full document please download

Php + Mysql

   EMBED


Share

Transcript

PHP + MySQL PHP PHP Marco Porta - Multimedia Systems and Technologies 1 Server-side technologies Apache, PHP, MySQL Download: • Apache Web Server: http://httpd.apache.org/download.cgi • PHP application server: http://www.php.net/downloads.php • MySQL DBMS: http://www.mysql.com/downloads/ • LAMP: is the integration of Linux, Apache, MySQL and PHP • Packages (Apache + PHP + MySQL in a single installer): • WAMP (Windows), (Wi d ) http://www.wampserver.com/en/ htt // / / • MAMP (Mac), http://www.mamp.info/en/ Marco Porta - Multimedia Systems and Technologies 2 1 Application server PHP Variables: $num = 58; $txt = "Hello World"; Operators: practically, the same as JavaScript (and Java, C, C++, …) Conditional constructs if (condition) …; elseif (condition) …; else …; Marco Porta - Multimedia Systems and Technologies 3 Application server PHP Arrays: • $cars = array("Saab","Volvo","BMW","Toyota"); • $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; Associative arrays: • $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); • $ $ages['Peter'] ['P t '] = "32" "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; Also multidimensional… Marco Porta - Multimedia Systems and Technologies 4 2 Application server PHP Loops: while (condition) { ...; } do { ...; } while (condition); for (init; condition; increment) { ...; } foreach ($array as $value) { ...; } Marco Porta - Multimedia Systems and Technologies 5 Application server PHP Functions: Marco Porta - Multimedia Systems and Technologies 6 3 Application server PHP include() function: ...
Content

Menu

... Marco Porta - Multimedia Systems and Technologies 7 Application server PHP File management: • $file = fopen("welcome.txt","r"); (r = Read only, starts at the beginning of the file; r+ = Read/Write, starts at the beginning of the file; w = write only, opens and clears the contents of file or creates a new file if it doesn't exist; w+ = read/write, opens and clears the contents of file or creates a new file if it doesn't exist; a = Append, opens and writes to the end of the file or creates a new file if it doesn't exist; a+ = Read/Append, preserves file content by writing to the end of the file; x = Write only, creates a new file, returns FALSE and an error if file already exists; x+ = Read/Write, creates a new file, returns FALSE and an error if file already exists) • fclose($file); c ose($ e); • Row-by-row reading: while(!feof($file)) { echo fgets($file). "
"; } Marco Porta - Multimedia Systems and Technologies 8 4 Database MySQL Opening a connection: Closing a connection: mysql_close($con); Database creation: if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } Marco Porta - Multimedia Systems and Technologies 9 Database MySQL Table creation: C CREATE TABLE tab table e_name a e ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... ) Entering data: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) Marco Porta - Multimedia Systems and Technologies 10 5 Database MySQL Example (table creation): Marco Porta - Multimedia Systems and Technologies 11 Database MySQL Example (data entering): Marco Porta - Multimedia Systems and Technologies 12 6 Database MySQL Example (entering data received from a form):
Firstname: Lastname: Age:
Marco Porta - Multimedia Systems and Technologies 13 Database MySQL Example(Select): "; } mysql_close($con); ?> Marco Porta - Multimedia Systems and Technologies 14 7