Short Intro to PHP and MySQL

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Short Intro to PHP and MySQL - Presentation Transcript

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

    + pohjuspohjus, 2 years ago

    custom

    1473 views, 1 favs, 4 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1473
      • 1397 on SlideShare
      • 76 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 61
    Most viewed embeds
    • 73 views on http://php.tpu.fi
    • 1 views on http://fachak.biz
    • 1 views on http://www.fachak.com
    • 1 views on http://imp-35.ning.com

    more

    All embeds
    • 73 views on http://php.tpu.fi
    • 1 views on http://fachak.biz
    • 1 views on http://www.fachak.com
    • 1 views on http://imp-35.ning.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories