Introduction To Lamp P2


Published on

This is the 2nd part of 'Introduction to LAMP' ..

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction To Lamp P2

  1. 1. Muhammad Abdur Rahman AdnaN [email_address]
  2. 2. <ul><li>COMP519: Web Programming (University of Liverpool) </li></ul><ul><ul><li> </li></ul></ul><ul><li> </li></ul>
  3. 3. <ul><li>SQL stands for Structured Query Language </li></ul><ul><li>using SQL can you can </li></ul><ul><ul><li>access a database </li></ul></ul><ul><ul><li>execute queries , and retrieve data </li></ul></ul><ul><ul><li>insert, delete and update records </li></ul></ul><ul><li>SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, mySQL, Postgre etc. </li></ul>
  4. 4. <ul><li>A database most often contains one or more tables. Each table is identified by a name (e.g. &quot;Customers&quot; or &quot;Orders&quot;). Tables contain records (rows) with data. </li></ul>The table above contains two records (one for each blog) and four columns For example, a table called “ Blog &quot;: Id Title Body Publish_date 1 Good morning How are you today? 2008-02-22 08:10:00 2 Good night How was your day? 2008-02-22 18:25:00
  5. 5. <ul><li>With SQL, you can query a database and have a result set returned. </li></ul>A query like this: SELECT title FROM blog ; Gives a result set like this: The mySQL database system requires a semicolon at the end of the SQL statement! Title Good morning Good night
  6. 6. <ul><li>The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted : </li></ul><ul><li>CREATE TABLE </li></ul><ul><li>ALTER TABLE </li></ul><ul><li>DROP TABLE </li></ul><ul><li>The query and update commands together form the Data Manipulation Language (DML) part of SQL : </li></ul><ul><li>SELECT </li></ul><ul><li>UPDATE </li></ul><ul><li>DELETE </li></ul><ul><li>INSERT INTO </li></ul>*Here we will use some of them in mySQL
  7. 7. <ul><li>You can log into our mySQL server from Linux by typing in the prompt </li></ul>From here you can create and drop tables, and modify the data in your tables. But first, you must specify which database on the server you want to use. bash-2.05b$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 209201 to server version: 5.0.22 Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> mysql> use myDatabase; Database changed
  8. 8. <ul><li>Next, you can create the table you will be using for the upcoming project. For example, </li></ul>mysql> CREATE TABLE blog ( -> id INT NOT NULL AUTO_INCREMENT, -> title VARCHAR(48), -> body VARCHAR(48), -> publish_date DATETIME, -> PRIMARY KEY(id)); Hit Enter after each line (if you want). MySQL doesn’t try to interpret the command itself until it sees a semicolon ( ; ) Query OK, 0 rows affected (0.02 sec) *If the server gives you a big ERROR, just try again from the top!
  9. 9. <ul><li>Using DESCRIBE you can see the structure of a table </li></ul>mysql> DESCRIBE blog ; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | title | varchar(48) | YES | | NULL | | | body | varchar(48) | YES | | NULL | | | publish_date | DATETIME | YES | | NULL | | +------------+-------------+------+-----+---------+----------------+
  10. 10. <ul><li>Using INSERT INTO you can insert a new row into your table. For example, </li></ul>mysql> INSERT INTO blog -> VALUES( ‘’ , ’Good Afternoon’ , ’Had your Lunch’ , NOW() ); Query OK, 1 row affected (0.00 sec) Using SELECT FROM you select some data from a table. mysql> SELECT * FROM blog ; +-----+---------+--------+------------+----------------------+ | id | title | body | publish_date | +-----+---------+--------+------------+----------------------+ | 1 | Good Afternoon | Had your Lunch | 2008-02-22 12:20:20 | +-----+---------+--------+------------+----------------------+ 1 row in set (0.00 sec)
  11. 11. <ul><li>You can repeat inserting until all data is entered into the table. </li></ul>mysql> INSERT INTO blog -> VALUES( ‘’ , ’How are you?’ , ’Doing fine?’ , NOW() ); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM blog ; +-----+---------+--------+------------+----------------------+ | id | title | body | publish_date | +-----+---------+--------+------------+----------------------+ | 1 | Good Afternoon | Had your Lunch | 2008-02-22 12:20:20 | | 2 | How are you?| Doing fine? | 2008-02-22 18:10:25 | +-----+---------+--------+------------+----------------------+ 2 rows in set (0.00 sec)
  12. 12. <ul><li>The SELECT command is the main way of getting data out of a table, or set of tables. </li></ul><ul><li>SELECT * FROM blog ; </li></ul><ul><li>Here the asterisk means to select (i.e. return the information in) all columns. </li></ul><ul><li>You can specify one or more columns of data that you like, such as </li></ul><ul><li>SELECT title, body FROM blog ; </li></ul><ul><li>+-------------+--------+ </li></ul><ul><li>| title | body | </li></ul><ul><li>+-------------+--------+ </li></ul><ul><li>| Good Afternoon | Had your Lunch | </li></ul><ul><li>| How are you? | Doing fine? | </li></ul><ul><li>+---------+--------+ </li></ul><ul><li>2 rows in set (0.00 sec) </li></ul>
  13. 13. <ul><li>You can specify other information that you want in the query using the WHERE clause. </li></ul><ul><li>SELECT * FROM blog WHERE id=‘2’ ; </li></ul><ul><li>+-----+---------+--------+------------+----------------------+ </li></ul><ul><li>| num | title | body | publish_date | </li></ul><ul><li>+-----+---------+--------+------------+----------------------+ </li></ul><ul><li>| 2 | Good Afternoon |Had your Lunch | 2008-02-22 12:20:20 | </li></ul><ul><li>+-----+---------+--------+------------+----------------------+ </li></ul><ul><li>1 row in set (0.00 sec) </li></ul>
  14. 14. <ul><li>The UPDATE statement is used to modify the data in a table. </li></ul>mysql> UPDATE blog SET publish_date='2008-02-23' WHERE id=1 ; mysql> SELECT * FROM blog ; +-----+---------+--------+------------+----------------------+ | id | title | body | publish_date | +-----+---------+--------+------------+----------------------+ | 1 | Good Afternoon | Had your Lunch | 2008-02-23 00:00:00| | 2 | How are you?| Doing fine? | 2008-02-22 18:10:25| +-----+---------+--------+------------+----------------------+ 2 rows in set (0.00 sec) Note that the date is expected in the format “YYYY-MM-DD” and I don’t believe this default setting can be changed. Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
  15. 15. <ul><li>The DELETE statement is used to delete rows in a table. </li></ul>mysql> DELETE FROM blog WHERE id=‘2' ; mysql> SELECT * FROM blog ; +-----+-------------------+------------+----------+------------------+ | id | title | body | publish_date | +-----+-------------------+------------+----------+------------------+ | 1 | Good Afternoon | Had your Lunch | 2008-02-23 00:00:00| +-----+-------------------+------------+----------------------+------------+ 1 row in set (0.00 sec) Query OK, 1 row affected (0.00 sec)
  16. 16. <ul><li>SHOW tables; gives a list of tables that have been defined in the database </li></ul><ul><li>ALTER TABLE blog DROP publish_date ; would drop the “publish_date” column from all records </li></ul><ul><li>DROP TABLE blog ; deletes the entire “blog” table, and its definition (use the DROP command with extreme care!!) </li></ul><ul><li>DELETE FROM blog ; removes all rows from the “blog” table (so once again, use the DELETE command with great caution), the table definition remains to be used again </li></ul><ul><li>A more useful(?) command is something like </li></ul><ul><li>DELETE FROM blog WHERE (id > 5) AND (id <= 10); </li></ul><ul><li>which selectively deletes blog based on their “id” values (for example). </li></ul><ul><li>HELP; gives the SQL help </li></ul><ul><li>HELP DROP; gives help on the DROP command, etc. </li></ul>
  17. 17. <ul><li>We can simply use PHP functions and mySQL queries together: </li></ul><ul><ul><li>Host: localhost </li></ul></ul><ul><ul><li>Database: bdosdn </li></ul></ul><ul><ul><li>Username: root </li></ul></ul><ul><ul><li>Password: <blank> </li></ul></ul><ul><li>Connect to the database server and login </li></ul><ul><li>mysql_connect( &quot; host &quot;,&quot; username &quot;,&quot; password &quot; ); </li></ul><ul><li>Choose the database </li></ul><ul><ul><li>mysql_select_db( &quot; database &quot; ); </li></ul></ul><ul><ul><li>Send SQL queries to the server to add, delete, and modify data </li></ul></ul><ul><ul><li>mysql_query( &quot; query &quot; ); </li></ul></ul><ul><li>Close the connection to the database server </li></ul><ul><li>mysql_close(); </li></ul>
  18. 18. <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Putting Data in the DB</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>/*insert blogs into DB*/ </li></ul><ul><li>if(isset($_POST[&quot;submit&quot;])) { </li></ul><ul><li>$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;root&quot;) or die('DB connection error!'); </li></ul><ul><li>mysql_select_db(&quot;bdosdn&quot;); </li></ul><ul><li>$date=date(&quot;Y-m-d&quot;); /* Get the current date in the right SQL format */ </li></ul><ul><li>$sql=&quot;INSERT INTO blog VALUES('','&quot; . $_POST[&quot;title&quot;] . &quot;','&quot; . $_POST[&quot;body&quot;] . &quot;', NOW())&quot;; /* construct the query */ </li></ul><ul><li>mysql_query($sql); </li></ul><ul><li>mysql_close(); </li></ul><ul><li>echo&quot;<h3>Thank you. The data has been entered.</h3> &quot;; </li></ul><ul><li>echo'<p><a href=&quot;blog_entry.php&quot;>Back to post</a></p>' . &quot; &quot;; </li></ul><ul><li>echo'<p><a href=&quot;blog_view.php &quot;>View the blog lists</a></p>' .&quot; &quot;; </li></ul><ul><li>} </li></ul>Blog_entry.php
  19. 19. <ul><li>else { </li></ul><ul><li>?> </li></ul><ul><li><h3>Enter your blog post into the database</h3> </li></ul><ul><li><form action=&quot;blog_entry.php&quot; method=&quot;POST&quot;> </li></ul><ul><li>Title: <input type=&quot;text&quot; name=&quot;title&quot; /> <br/> </li></ul><ul><li>Body: <input type=&quot;textarea&quot; name=&quot;body&quot; /> <br/> </li></ul><ul><li><input type=&quot;submit&quot; name=&quot;submit&quot; /> <input type=&quot;reset&quot; /> </li></ul><ul><li></form> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>Blog_entry.php
  20. 20. <ul><li>Similarly, we can get some information from a database: </li></ul><ul><li>Connect to the server and login, choose a database </li></ul><ul><li>mysql_connect( &quot; host &quot;,&quot; username &quot;,&quot; password &quot; ); </li></ul><ul><li>mysql_select_db( &quot; database &quot; ); </li></ul><ul><ul><li>Send an SQL query to the server to select data from the database into an array </li></ul></ul><ul><ul><li>$result =mysql_query( &quot; query &quot; ); </li></ul></ul><ul><ul><li>Either, look into a row and a fieldname </li></ul></ul><ul><ul><li>$num =mysql_numrows( $result ); </li></ul></ul><ul><ul><li>$variable =mysql_result( $result , $i ,&quot; fieldname &quot; ); </li></ul></ul><ul><ul><ul><li>Or, fetch rows one by one </li></ul></ul></ul><ul><ul><li>$row =mysql_fetch_array( $result ); </li></ul></ul><ul><li>Close the connection to the database server </li></ul><ul><li>mysql_close(); </li></ul>
  21. 21. <html> <head> <title>Getting Data out of the DB</title> </head> <body> <h1> Blog Database </h1> <?php /*get blog from the DB */ $db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;root&quot;) or die('DB connection error!'); mysql_select_db(&quot;bdosdn&quot;, $db); $sql = &quot;SELECT * FROM blog ORDER BY id desc&quot;; $result=mysql_query($sql); while($row=mysql_fetch_array($result)){ echo &quot;<h4> Title: &quot; . $row[&quot;title&quot;] . &quot;</h4> &quot;; echo &quot;<h5> Publish Date: &quot; . $row[&quot;publish_date&quot;] . &quot;<br/> Body: &quot; . $row[&quot;body&quot;] . &quot;</h5> &quot;; echo '<hr>'; } mysql_close(); ?> </body> </html> Blog_view.php
  22. 22. <ul><li>Can create a table </li></ul><ul><li>Can delete rows and columns </li></ul><ul><li>Can make updates </li></ul><ul><li>Can make queries to several tables </li></ul><ul><li>Can get connected to several databases </li></ul>* Find more information on PHP/mySQL
  23. 23. <ul><li>In these last lectures you have learned </li></ul><ul><li>What is SQL </li></ul><ul><li>How to access mySQL database </li></ul><ul><li>How to create a basic mySQL database </li></ul><ul><li>How to use some basic queries </li></ul><ul><li>How to use PHP and mySQL </li></ul>