Short Intro to PHP and MySQL

5,947 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,947
On SlideShare
0
From Embeds
0
Number of Embeds
90
Actions
Shares
0
Downloads
425
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Short Intro to PHP and MySQL

  1. 1. SQL, MySQL and PHP Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. Three-tiered Web Site: LAMP Client User-agent: Firefox Server Apache HTTP Server example request GET / HTTP/1.1 Host: www.tamk.fi User-Agent: Mozilla/5.0 (Mac..) ... response Database MySQL PHP
  3. 3. DATABASES AND SQL
  4. 4. Database Management System ( MySQL ) Database Software for Managing the Database Querys Users
  5. 5. SQL - Database <ul><li>SQL – is a database computer language used in relational databases </li></ul><ul><ul><li>S tructured Q uery L anguage </li></ul></ul><ul><li>ANSI and ISO – standard </li></ul><ul><ul><li>SQL-86 </li></ul></ul><ul><ul><li>SQL-89 </li></ul></ul><ul><ul><li>SQL-92 </li></ul></ul><ul><ul><li>SQL-1999 </li></ul></ul><ul><ul><li>SQL-2003 </li></ul></ul><ul><ul><li>SQL-2006 </li></ul></ul><ul><li>See features: </li></ul><ul><ul><li>http://en.wikipedia.org/wiki/Sql#Standardization </li></ul></ul>
  6. 6. SQL - table <ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| id | firstname | lastname | email | mobile | gender | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| 0 | Pekka | Virtanen | pekka.virtanen@foo.fi | +35840123456 | m | </li></ul><ul><li>| 1 | Tiina | Tampio | tiina.tampio@foo.fi | +35848763324 | f | </li></ul><ul><li>| 2 | Kaisa | Kekkonen | kaisa.kekkonen@foo.fi | +35840123456 | f | </li></ul><ul><li>| 3 | Pasi | Pesukarhu | pasi.pesukarhu@foo.fi | +358412345 | m | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul>
  7. 7. Example of Creating Table <ul><li>CREATE TABLE employees ( </li></ul><ul><li>id INTEGER, </li></ul><ul><li>name VARCHAR(20), </li></ul><ul><li>salary DECIMAL(8,2), </li></ul><ul><li>PRIMARY KEY (id) </li></ul><ul><li>); </li></ul>
  8. 8. Datatypes <ul><li>INTEGER </li></ul><ul><li>DECIMAL(L,S) </li></ul><ul><li>CHAR(N) </li></ul><ul><li>VARCHAR(N) </li></ul><ul><li>... </li></ul>
  9. 9. Primary Key <ul><li>With primary key you differentiate rows from each other </li></ul><ul><li>Only one primary key for the table </li></ul>
  10. 10. Remove the Table <ul><li>DROP TABLE employees; </li></ul>
  11. 11. Adding Information to Table <ul><li>INSERT INTO employees VALUES ( </li></ul><ul><li>1, </li></ul><ul><li>'Jack North', </li></ul><ul><li>3000 </li></ul><ul><li>); </li></ul>
  12. 12. Deleting Information from Table <ul><li>-- Delete all rows </li></ul><ul><li>DELETE FROM employees; </li></ul><ul><li>-- Delete one row </li></ul><ul><li>DELETE FROM employees </li></ul><ul><li>WHERE id = 1; </li></ul>
  13. 13. Retrieving Information <ul><li>SELECT * </li></ul><ul><li>FROM employees; </li></ul><ul><li>-- </li></ul><ul><li>SELECT name, salary </li></ul><ul><li>FROM employees; </li></ul><ul><li>-- </li></ul><ul><li>SELECT name </li></ul><ul><li>FROM employees </li></ul><ul><li>WHERE id = 1; </li></ul><ul><li>SELECT * </li></ul><ul><li>FROM employees </li></ul><ul><li>WHERE id = 1 AND </li></ul><ul><li>palkka < 4000; </li></ul>
  14. 14. Multiple Tables <ul><li>select * from client; </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| id | firstname | lastname | email | mobile | gender | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| 0 | Pekka | Virtanen | pekka.virtanen@foo.fi | +35840123456 | m | </li></ul><ul><li>| 1 | Tiina | Tampio | tiina.tampio@foo.fi | +35848763324 | f | </li></ul><ul><li>| 2 | Kaisa | Kekkonen | kaisa.kekkonen@foo.fi | +35840123456 | f | </li></ul><ul><li>| 3 | Pasi | Pesukarhu | pasi.pesukarhu@foo.fi | +358412345 | m | </li></ul><ul><li>| 4 | Foo | Bar | foo@bar.baz | +35850505050 | f | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul>
  15. 15. Multiple Tables <ul><li>select * from product; </li></ul><ul><li>+----+-------+-------+ </li></ul><ul><li>| id | name | price | </li></ul><ul><li>+----+-------+-------+ </li></ul><ul><li>| 1 | Milk | 0.70 | </li></ul><ul><li>| 2 | Bread | 2.00 | </li></ul><ul><li>| 3 | Egg | 1.50 | </li></ul><ul><li>+----+-------+-------+ </li></ul>
  16. 16. Multiple Tables <ul><li>select * from purchase; </li></ul><ul><li>+----------+-----------+ </li></ul><ul><li>| clientID | productID | </li></ul><ul><li>+----------+-----------+ </li></ul><ul><li>| 1 | 1 | </li></ul><ul><li>| 2 | 1 | </li></ul><ul><li>| 2 | 2 | </li></ul><ul><li>+----------+-----------+ </li></ul><ul><li>3 rows in set (0.00 sec) </li></ul>
  17. 17. Relationship <ul><li>select * from client; </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| id | firstname | lastname | email | mobile | gender | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul><ul><li>| 0 | Pekka | Virtanen | pekka.virtanen@foo.fi | +35840123456 | m | </li></ul><ul><li>| 1 | Tiina | Tampio | tiina.tampio@foo.fi | +35848763324 | f | </li></ul><ul><li>| 2 | Kaisa | Kekkonen | kaisa.kekkonen@foo.fi | +35840123456 | f | </li></ul><ul><li>| 3 | Pasi | Pesukarhu | pasi.pesukarhu@foo.fi | +358412345 | m | </li></ul><ul><li>| 4 | Foo | Bar | foo@bar.baz | +35850505050 | f | </li></ul><ul><li>+----+-----------+-----------+-----------------------+--------------+--------+ </li></ul>select * from product; +----+-------+-------+ | id | name | price | +----+-------+-------+ | 1 | Milk | 0.70 | | 2 | Bread | 2.00 | | 3 | Egg | 1.50 | +----+-------+-------+ select * from purchase; +----------+-----------+ | clientID | productID | +----------+-----------+ | 1 | 1 | | 2 | 1 | | 2 | 2 | +----------+-----------+ 3 rows in set (0.00 sec)
  18. 18. Query <ul><li>mysql> SELECT client.firstname, client.lastname, product.name </li></ul><ul><li>-> FROM client, purchase, product </li></ul><ul><li>-> WHERE client.id = purchase.clientid AND </li></ul><ul><li>-> purchase.productid = product.id; </li></ul><ul><li>+-----------+----------+-------+ </li></ul><ul><li>| firstname | lastname | name | </li></ul><ul><li>+-----------+----------+-------+ </li></ul><ul><li>| Tiina | Tampio | Milk | </li></ul><ul><li>| Kaisa | Kekkonen | Milk | </li></ul><ul><li>| Kaisa | Kekkonen | Bread | </li></ul><ul><li>+-----------+----------+-------+ </li></ul>
  19. 19. MYSQL
  20. 20. MySQL <ul><li>Very popular SQL-database </li></ul><ul><li>Was developed by Swedish company MySQL AB </li></ul><ul><li>In 2008 Sun Microsystems bought MySQL AB </li></ul><ul><ul><li>Both GNU GPL and Commercial Licence </li></ul></ul><ul><li>Has different UIs: CLI, WEB and GUI </li></ul><ul><li>MySQL is used by Google, Wikipedia and Yahoo. </li></ul>
  21. 21. MySQL Usage via CLI (TAMK) <ul><li>Login to gamma </li></ul><ul><ul><li>ssh loginname@tpu.fi </li></ul></ul><ul><li>Login to MySQL </li></ul><ul><ul><li>mysql –h myX.tpu.fi –u loginname -p databasename </li></ul></ul><ul><li>See the databases </li></ul><ul><ul><li>show databases; </li></ul></ul><ul><li>Change database </li></ul><ul><ul><li>use databasename; </li></ul></ul><ul><li>Exit </li></ul><ul><ul><li>quit </li></ul></ul>
  22. 22. SSH – connection and MySQL
  23. 23. MySQL Tutorial <ul><li>MySQL Tutorial can be found: </li></ul><ul><ul><li>http://dev.mysql.com/doc/refman/5.0/en/tutorial.html </li></ul></ul>
  24. 24. PHP AND MYSQL
  25. 25. PHP and MySQL <ul><li>PHP has functions that allow access to MySQL Databases </li></ul><ul><li>Access is very easy </li></ul><ul><ul><li>1. Connect </li></ul></ul><ul><ul><li>2. Select Database </li></ul></ul><ul><ul><li>3. Query </li></ul></ul><ul><ul><li>4. Close connection </li></ul></ul>
  26. 26. 1. Connect <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul>
  27. 27. 2. Select Database <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul>
  28. 28. 3. Query <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot;DELETE FROM table;&quot;, $link); </li></ul>
  29. 29. 4. Close <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot;DELETE FROM table;&quot;, $link); </li></ul><ul><li>mysql_close($link); </li></ul>
  30. 30. mysql_query <ul><li>INSERT, UPDATE, DELETE, DROP </li></ul><ul><ul><li>Returns true or false </li></ul></ul><ul><li>SELECT, SHOW, DESCRIBE, EXPLAIN </li></ul><ul><ul><li>Returns resource on success, false on error </li></ul></ul><ul><ul><li>The returned resource should be passed to mysql_fetch_array() </li></ul></ul>
  31. 31. Retrieving Table <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot; SELECT * FROM table; &quot;, $link); </li></ul><ul><li>while ($row = mysql_fetch_array($result)) </li></ul><ul><li>{ </li></ul><ul><li>print($row[0]); </li></ul><ul><li>print($row[1]); </li></ul><ul><li>} </li></ul><ul><li>mysql_close($link); </li></ul>
  32. 32. Retrieving Table <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot; SELECT * FROM table; &quot;, $link); </li></ul><ul><li>while ($row = mysql_fetch_array($result)) </li></ul><ul><li>{ </li></ul><ul><li>print($row[&quot;id&quot;]); </li></ul><ul><li>print($row[&quot;name&quot;]); </li></ul><ul><li>} </li></ul><ul><li>mysql_close($link); </li></ul>
  33. 33. Retrieving Table <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot;SELECT * FROM table;&quot;, $link); </li></ul><ul><li>while ($row = mysql_fetch_array($result, MYSQL_BOTH )) </li></ul><ul><li>{ </li></ul><ul><li>print($row[0]); </li></ul><ul><li>print($row[&quot;name&quot;]); </li></ul><ul><li>} </li></ul><ul><li>mysql_close($link); </li></ul>
  34. 34. Retrieving Table <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot;SELECT * FROM table;&quot;, $link); </li></ul><ul><li>while ($row = mysql_fetch_array($result, MYSQL_NUM )) </li></ul><ul><li>{ </li></ul><ul><li>print($row[0]); // Only number indices </li></ul><ul><li>print($row[1]); </li></ul><ul><li>} </li></ul><ul><li>mysql_close($link); </li></ul>
  35. 35. Retrieving Table <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>$db_selected = mysql_select_db('foo', </li></ul><ul><li>$link); </li></ul><ul><li>$result = mysql_query(&quot;SELECT * FROM table;&quot;, $link); </li></ul><ul><li>while ($row = mysql_fetch_array($result, MYSQL_ASSOC )) </li></ul><ul><li>{ </li></ul><ul><li>print($row[&quot;id&quot;]); // Only associative indices </li></ul><ul><li>print($row[&quot;name&quot;]); </li></ul><ul><li>} </li></ul><ul><li>mysql_close($link); </li></ul>
  36. 36. Error Handling <ul><li>Every function in the previous code examples could fail </li></ul><ul><ul><li>Connection can fail, sql query can fail etc. </li></ul></ul><ul><li>Usually you exit the script when DB fails. </li></ul><ul><li>With exit($status) – function, you can stop the execution of the script. </li></ul>
  37. 37. Example of Error Handling 1 <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password'); </li></ul><ul><li>if( ! $link ) </li></ul><ul><li>{ </li></ul><ul><li>exit(&quot;Error connecting to database&quot;); </li></ul><ul><li>} </li></ul>
  38. 38. Example of Error Handling 2 <ul><li>$link = mysql_connect('mysql_server', </li></ul><ul><li>'mysql_user', </li></ul><ul><li>'mysql_password') </li></ul><ul><li>or exit(&quot;Error connecting to database&quot;); </li></ul>
  39. 39. DESIGNING WEB APPLICATION WITH DB CONNECTION
  40. 40. Maintanence <ul><li>Implement app with the point of view of maintanence </li></ul><ul><li>What if the http server is changed? </li></ul><ul><li>What if the database server is changed? </li></ul><ul><li>What if you have to change the code in your app? </li></ul><ul><li>What if someone else have to change the code in the app? </li></ul>
  41. 41. Configuration files for DB <ul><li>Create special configuration file for Database configuration. </li></ul><ul><li>This file should have constants just for the database connection </li></ul><ul><li>You could name it conf/database.php </li></ul>
  42. 42. Example of conf/database.php <ul><li><?php </li></ul><ul><li>/** </li></ul><ul><li>* database.php - holds necessary constants for database connection </li></ul><ul><li>* </li></ul><ul><li>* Copyright information </li></ul><ul><li>* </li></ul><ul><li>* Copyright (C) 2008 Jussi Pohjolainen <first.last@tamk.fi> </li></ul><ul><li>* </li></ul><ul><li>* License </li></ul><ul><li>* </li></ul><ul><li>* Here should be the license... </li></ul><ul><li>* </li></ul><ul><li>*/ </li></ul><ul><li>define(&quot;MYSQL_HOST&quot;, &quot;myX.tpu.fi&quot;); </li></ul><ul><li>define(&quot;MYSQL_USER&quot;, &quot;pohjus&quot;); </li></ul><ul><li>define(&quot;MYSQL_PASSWD&quot;, &quot;mypassword&quot;); </li></ul><ul><li>define(&quot;MYSQL_DB&quot;, &quot;dbpohjus&quot;); </li></ul><ul><li>// End of file </li></ul><ul><li>?> </li></ul>
  43. 43. Database Instructions <ul><li>Create instructions for the admin </li></ul><ul><ul><li>How to create the database </li></ul></ul><ul><ul><li>How to put example data into the database </li></ul></ul><ul><ul><li>Hot to delete the database </li></ul></ul><ul><li>You could create directory just for this: </li></ul><ul><ul><li>doc/sql </li></ul></ul><ul><ul><ul><li>install.txt </li></ul></ul></ul><ul><ul><ul><li>table-create.sql </li></ul></ul></ul><ul><ul><ul><li>table-drop.sql </li></ul></ul></ul><ul><ul><ul><li>data-insert.sql </li></ul></ul></ul><ul><ul><ul><li>data-delete.sql </li></ul></ul></ul><ul><ul><ul><li>select1.sql </li></ul></ul></ul>
  44. 44. table-create.sql <ul><li>-- table-create.sql This will create neccessary clients-table </li></ul><ul><li>-- </li></ul><ul><li>-- COPYRIGHT </li></ul><ul><li>-- Copyright information here </li></ul><ul><li>-- LICENCE </li></ul><ul><li>-- Licence information here </li></ul><ul><li>-- DESCRIPTON </li></ul><ul><li>-- Detailed description here </li></ul><ul><li>CREATE TABLE clients </li></ul><ul><li>( </li></ul><ul><li>id INTEGER PRIMARY KEY, </li></ul><ul><li>firstname VARCHAR(64) NOT NULL, </li></ul><ul><li>... </li></ul><ul><li>); </li></ul><ul><li>-- End of file </li></ul>
  45. 45. install.txt <ul><li>FILE IDENTIFICATION </li></ul><ul><li>File : install.txt </li></ul><ul><li>Time-stamp : <2008-10-17 14:36:53 Jussi Pohjolainen> </li></ul><ul><li>Description: Database install, test and remove instructions </li></ul><ul><li>TO INSTALL THE DATABASE </li></ul><ul><li>Database is installed in following steps: </li></ul><ul><li>1. ... </li></ul><ul><li>EXAMPLE DATA <for testing or product setup> </li></ul><ul><li>An example database can be installed in steps: </li></ul><ul><li>1. ... </li></ul><ul><li>TO REMOVE THE DATABASE </li></ul>
  46. 46. Making Querys <ul><li>Create your own class that has Database related methods </li></ul><ul><li>These methods can handles also the errors and exceptions </li></ul>
  47. 47. Example of Usage: lib/include.php <ul><li><?php </li></ul><ul><li>/** </li></ul><ul><li>* include.php - file that includes all the necessary files </li></ul><ul><li>* </li></ul><ul><li>* Copyright information </li></ul><ul><li>* Copyright (C) 2008 Jussi Pohjolainen <firstname.lastname@tamk.fi> </li></ul><ul><li>* </li></ul><ul><li>* License </li></ul><ul><li>* License here </li></ul><ul><li>*/ </li></ul><ul><li>// Import basic configuration file </li></ul><ul><li>require_once(&quot;conf/application.php&quot;); </li></ul><ul><li>// Import database configuration file </li></ul><ul><li>require_once(&quot;conf/database.php&quot;); </li></ul><ul><li>// Import Database - class </li></ul><ul><li>require_once(&quot;class/Database.php&quot;); </li></ul><ul><li>// Import Table - class </li></ul><ul><li>require_once(&quot;class/Table.php&quot;); </li></ul><ul><li>// Import HTMLGenerator- class </li></ul><ul><li>require_once(&quot;class/HTMLGenerator.php&quot;); </li></ul><ul><li>// End of file </li></ul><ul><li>?> </li></ul>
  48. 48. Example of Usage: index.php <ul><li><?php </li></ul><ul><li>/** </li></ul><ul><li>* index.php – UI for the app </li></ul><ul><li>* </li></ul><ul><li>* Copyright information + Licence here </li></ul><ul><li>*/ </li></ul><ul><li>// Import all necessary files </li></ul><ul><li>require_once(&quot;include.php&quot;); </li></ul><ul><li>function main() </li></ul><ul><li>{ </li></ul><ul><li>print HTMLGenerator::myHeader( TITLE, STYLESHEET ); </li></ul><ul><li>$result = Database::execute( &quot;select * from clients&quot; ); </li></ul><ul><li> print Table::toXhtmlTable( $result ); </li></ul><ul><li> print HTMLGenerator::myFooter(); </li></ul><ul><li>} </li></ul><ul><li>main(); </li></ul><ul><li>// End of file </li></ul><ul><li>?> </li></ul>

×