Introduction To Lamp P2

24
Muhammad Abdur Rahman AdnaN jhoroTEK.com [email protected]

description

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

Transcript of Introduction To Lamp P2

Page 1: Introduction To Lamp P2

Muhammad Abdur Rahman [email protected]

Page 2: Introduction To Lamp P2

COMP519: Web Programming (University of Liverpool) http://www.csc.liv.ac.uk/~martin/teaching/comp51

9/ http://www.w3schools.com

Page 3: Introduction To Lamp P2

SQL stands for Structured Query Language

using SQL can you can access a database execute queries, and retrieve data insert, delete and update records

SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, mySQL, Postgre etc.

Page 4: Introduction To Lamp P2

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

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

The table above contains two records (one for each blog) and four columns

For example, a table called “Blog":

Page 5: Introduction To Lamp P2

With SQL, you can query a database and have a result set returned.

Title

Good morning

Good night

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!

Page 6: Introduction To Lamp P2

The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted:• CREATE TABLE• ALTER TABLE• DROP TABLE

The query and update commands together form the Data Manipulation Language (DML) part of SQL:• SELECT• UPDATE• DELETE• INSERT INTO

*Here we will use some of them in mySQL

Page 7: Introduction To Lamp P2

You can log into our mySQL server from Linux by typing in the prompt

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>

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.

mysql> use myDatabase;

Database changed

Page 8: Introduction To Lamp P2

Next, you can create the table you will be using for the upcoming project. For example,

mysql> CREATE TABLE blog(

-> id INT NOT NULL AUTO_INCREMENT,

-> title VARCHAR(48),

-> body VARCHAR(48),

-> publish_date DATETIME,

-> PRIMARY KEY(id));

Query OK, 0 rows affected (0.02 sec)

*If the server gives you a big ERROR, just try again from the top!

Hit Enter after each line (if you want). MySQL doesn’t try to interpret the command itself until it sees a semicolon (;)

Page 9: Introduction To Lamp P2

Using DESCRIBE you can see the structure of a table

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 | |+------------+-------------+------+-----+---------+----------------+

Page 10: Introduction To Lamp P2

Using INSERT INTO you can insert a new row into your table. For example,

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)

Page 11: Introduction To Lamp P2

You can repeat inserting until all data is entered into the table.

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)

Page 12: Introduction To Lamp P2

The SELECT command is the main way of getting data out of a table, or set of tables.

SELECT * FROM blog; Here the asterisk means to select (i.e. return the

information in) all columns.

You can specify one or more columns of data that you like, such as

SELECT title, body FROM blog; +-------------+--------+ | title | body | +-------------+--------+ | Good Afternoon | Had your Lunch | | How are you? | Doing fine? | +---------+--------+ 2 rows in set (0.00 sec)

Page 13: Introduction To Lamp P2

You can specify other information that you want in the query using the WHERE clause.

SELECT * FROM blog WHERE id=‘2’; +-----+---------+--------+------------+----------------------+

| num | title | body | publish_date |

+-----+---------+--------+------------+----------------------+

| 2 | Good Afternoon |Had your Lunch | 2008-02-22 12:20:20 |

+-----+---------+--------+------------+----------------------+

1 row in set (0.00 sec)

Page 14: Introduction To Lamp P2

The UPDATE statement is used to modify the data in a table.

mysql> UPDATE blog SET publish_date='2008-02-23' WHERE id=1;

Query OK, 1 row affected (0.01 sec)Rows matched: 1 Changed: 1 Warnings: 0

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 thisdefault setting can be changed.

Page 15: Introduction To Lamp P2

The DELETE statement is used to delete rows in a table.

mysql> DELETE FROM blog WHERE id=‘2';

Query OK, 1 row affected (0.00 sec)

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)

Page 16: Introduction To Lamp P2

SHOW tables; gives a list of tables that have been defined in the database

ALTER TABLE blog DROP publish_date; would drop the “publish_date” column from all records

DROP TABLE blog; deletes the entire “blog” table, and its definition (use the DROP command with extreme care!!)

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

A more useful(?) command is something like DELETE FROM blog WHERE (id > 5) AND (id <= 10); which selectively deletes blog based on their “id” values (for

example).

HELP; gives the SQL help HELP DROP; gives help on the DROP command, etc.

Page 17: Introduction To Lamp P2

We can simply use PHP functions and mySQL queries together:

Host: localhost

Database: bdosdn

Username: root

Password: <blank>

• Connect to the database server and login

mysql_connect("host","username","password");

• Choose the database

mysql_select_db("database");

• Send SQL queries to the server to add, delete, and modify data

mysql_query("query");

• Close the connection to the database server

mysql_close();

Page 18: Introduction To Lamp P2

<html><head>

<title>Putting Data in the DB</title>

</head>

<body>

<?php

/*insert blogs into DB*/

if(isset($_POST["submit"])) {

$db = mysql_connect("localhost", "root", "root") or die('DB connection error!');

mysql_select_db("bdosdn");

$date=date("Y-m-d"); /* Get the current date in the right SQL format */

$sql="INSERT INTO blog VALUES('','" . $_POST["title"] . "','" . $_POST["body"] . "', NOW())"; /* construct the query */

mysql_query($sql);

mysql_close();

echo"<h3>Thank you. The data has been entered.</h3> \n";

echo'<p><a href="blog_entry.php">Back to post</a></p>' . "\n";

echo'<p><a href="blog_view.php ">View the blog lists</a></p>' ."\n";

}

Blog_entry.php

Page 19: Introduction To Lamp P2

else {

?>

<h3>Enter your blog post into the database</h3>

<form action="blog_entry.php" method="POST">

Title: <input type="text" name="title" /> <br/>

Body: <input type="textarea" name="body" /> <br/>

<input type="submit" name="submit" /> <input type="reset" />

</form>

<?php

}

?>

</body>

</html>

Blog_entry.php

Page 20: Introduction To Lamp P2

Similarly, we can get some information from a database:• Connect to the server and login, choose a database

mysql_connect("host","username","password");

mysql_select_db("database");

• Send an SQL query to the server to select data from the database into an array

$result=mysql_query("query");

• Either, look into a row and a fieldname

$num=mysql_numrows($result);

$variable=mysql_result($result,$i,"fieldname");

• Or, fetch rows one by one

$row=mysql_fetch_array($result);• Close the connection to the database server

mysql_close();

Page 21: Introduction To Lamp P2

<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("localhost", "root", "root") or die('DB connection error!');mysql_select_db("bdosdn", $db);

$sql = "SELECT * FROM blog ORDER BY id desc";

$result=mysql_query($sql);while($row=mysql_fetch_array($result)){ echo "<h4> Title: " . $row["title"] . "</h4> \n"; echo "<h5> Publish Date: " . $row["publish_date"] . "<br/> Body: " . $row["body"] . "</h5> \

n"; echo '<hr>';}mysql_close();?>

</body></html>

Blog_view.php

Page 22: Introduction To Lamp P2

Can create a table Can delete rows and columns Can make updates Can make queries to several tables Can get connected to several databases

* Find more information on PHP/mySQL

Page 23: Introduction To Lamp P2

In these last lectures you have learned What is SQL How to access mySQL database How to create a basic mySQL database How to use some basic queries How to use PHP and mySQL

Page 24: Introduction To Lamp P2