0% found this document useful (0 votes)
28 views

Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST

This document provides an overview of server-side programming concepts including PHP, MySQL, Apache, and databases. It discusses what PHP and MySQL are and their purposes. It explains that a database server stores structured information that is accessible through programming languages like PHP. The document then covers installing and using MySQL, including how to log in, view existing databases, create a new database, and construct a table within that database with columns, data types, and other attributes. Examples are given of basic MySQL commands for tasks like selecting a database and creating, viewing, and deleting tables.

Uploaded by

Rifat Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST

This document provides an overview of server-side programming concepts including PHP, MySQL, Apache, and databases. It discusses what PHP and MySQL are and their purposes. It explains that a database server stores structured information that is accessible through programming languages like PHP. The document then covers installing and using MySQL, including how to log in, view existing databases, create a new database, and construct a table within that database with columns, data types, and other attributes. Examples are given of basic MySQL commands for tasks like selecting a database and creating, viewing, and deleting tables.

Uploaded by

Rifat Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Server Side Programming

By Dr. Babaousmail Hassen

Lecturer at Binjiang College of NUIST


Review

What is PHP?
Review

What is MySQL?
Review

What are the database


types?
Review

What is Apache?
Review

What is the name of


combined package for PHP,
Apache and MySQL in
window?
Introduction to Database Server
Introduction to Database
Server
Till now we have discussed the introduction of Apache, PHP, MySQL
and their installation

• A database server (in our case, MySQL) is a program that can


store large amounts of information in an organized format that’s
easily accessible through programming languages like PHP.
• For example, you could tell PHP to look in the database for a list
of students that you like to appear on your web site.
Advantages of Database Server

 Instead writing an HTML page for each of students, you could


write a single PHP script that to get any student information from
the database and display it by generating an HTML page.
 Second, adding a new student information to your web site would
be a simpler just by inserting the information of student into the
database.
Example of A Simple Database
• A database is composed of one or more tables.
• For example a student data base contains list of students and their
university id no. Each table in a database has one or more columns.
Each column holds a certain piece of information about each item.
In our example the first column is for the text of the names of
students, and another for their university id.
• Each student information is stored in a row or entry in the table.
• Notice that, in addition to columns for the name of students and
their university ID, I have included a column named id.
• The function of the id column, is to assign a unique number to each
student so that we have an easy way to refer and track them.
Example of A Simple Database

Id Name Student-Id

1 UMUKUNZI LILIANE 20155300011

2 NDUWAMUNGU MUSA 20155300054

3 RUHANGURA LIONEL 20155300062

4 YOSUA TANIA 20155300068


5 KIMANA MICHEL 20155300104
Logging On to MySQL
• Just as a web server is designed to respond to requests from a client (a web
browser),
• Similarly the MySQL database server responds to requests from client programs.
mysqladmin is an example of a MySQL client program. Another client program
that comes with the MySQL server is called mysql
• Open a Terminal window (or Command Prompt if you’re using a Windows system)
and type this command to run the mysql client program:
mysql --version
• If everything is set up right, this command should output a one-line description of
the version of the mysql client program that you have installed like on my window:
mysql Ver 14.14 Distrib 5.1.31, for Win 32 (ia32)
• If you receive an error message complaining that your computer is unable to
recognize the mysql command, you should probably revisit the installation
instructions provided in previous lectures.
 Assuming the mysql program is running for you, you can now use it to connect to
your MySQL server. First, make sure that server is running, then type this
command and hit Enter:
mysql -u root –p
 -u root tells the program you wish to connect to the server using the root user
account, and -p tells it you’re going to provide a password.

 You should see next is an Enter password: prompt. Enter the root password you
selected previously during the post-installation and hit Enter.

 If you typed your password correctly, you will see following message in your
command prompt
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.31 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>
Some Simple Command of
MySQL Server
Type this command (including the semicolon!) and press Enter:
mysql> SHOW DATABASES;
MySQL will show you a list of the databases on the server. If you’re
working on a
brand new server, the list should look like this:
Some Simple Command of
MySQL Server
• The MySQL server uses the first database, named
information_schema, to keep track of all the other databases on the
server.

• The second database, mysql, is special too. MySQL uses it to keep


track of users, their passwords.

• The third database, named test, is a sample database. You can


actually delete this database because I’ll show you how to create
your own database in a moment.
Some Simple Command of
MySQL Server
• Deleting stuff in MySQL is called “dropping” it, and the command
for doing so is as follows:
mysql> DROP DATABASE test;
• If you type this command and press Enter, MySQL will obediently
delete the database, displaying “Query OK” in confirmation.

• Notice that there’s no confirmation prompt like “Are you sure?”.


So you have to be very careful to type your commands correctly in
the mysql client program because, as this example shows, you can
destroy your entire database—along with all the information it
contains—with a single command!
Some Simple Command of
MySQL Server
Some basics about the MySQL command prompt. As you may have
noticed, all commands in MySQL are terminated by a semicolon (;).
But If you forget the semicolon, nothing goes wrong, MySQL will
think you are still typing your command, and will let you continue on
another line: you can test below example
mysql> SHOW
-> DATABASES;
MySQL shows that it’s waiting for you to type more of your
command by changing
the prompt from mysql> to -> (dash right arrow).
Some Simple Command of
MySQL Server
Now when you will press Enter:

MySQL will show you only two databases on the server. Since you deleted the third
database test.

If you’re halfway through a command and


realize that you made a mistake, and you may
want to cancel the current command entirely. To
do this, type \c and press Enter:

mysql> DROP DATABASE\c 2


mysql>
Some Simple Command of
MySQL Server

Finally, if at any time you want to exit the MySQL client program, just
type quit or exit (either will work). This is the only command where
the semicolon is unnecessary, but you can use one if you want to.

mysql> quit
Bye
Creating a Database

Since you used the quit command last time to you need to login again
on MySQL
to connect to your MySQL server. Type this command and hit Enter:
mysql -u root –p
Enter the root password, and hit Enter. You will see the mysql prompt
like
mysql>
Creating a Database

We can use USE command to use the created database as below

mysql> USE ijdb;

You will receive a message Database changed. Now your database is


ready to use. Since a database is empty until you add some tables to
it, so now we will create a table for database.
Creating a Database

It is just as easy to create a database as it is to delete one:

mysql> CREATE DATABASE ijdb;

I chose to name the database ijdb. You can chose any name for your
database.
Now that you have a database, you need to tell MySQL that you want
to use it.
Creating a Table

The basic form of the command is as follows:

mysql> CREATE TABLE table_name (


-> column1Name column1Type column1Details,
-> column2Name column2Type column2Details,
-> ⋮
-> ) DEFAULT CHARACTER SET charset;
Creating a Table (example)
See the Table above . It had three columns: id (a number), joke(the text), and
jokedate (the date on which the joke was entered).
This is the command to create the Table:
mysql> CREATE TABLE joke (
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> joketext TEXT,
-> jokedate DATE NOT NULL
-> ) DEFAULT CHARACTER SET utf8;
Creating a Table (Example) Discussion of commands
CREATE TABLE joke (
This line says that we want to create a new table named joke. The opening parenthesis
(() represents the beginning of the list of columns in the table.

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,


This second line says that we want a column called id that will contain an integer
(INT), that is, a whole number. The rest of this line deals with special details for the
column:

1. First, when creating a row in this table, this column is not allowed to be left blank
(NOT NULL).
2. Next, when we add a new entry to the table, we want MySQL to automatically pick
a value that is one more than the highest value in the table so far
(AUTO_INCREMENT).
3. Finally, this column is to act as a unique identifier for the entries in the table, so all
values in this column must be unique (PRIMARY KEY).
Creating a Table (Example) Discussion of commands
joketext TEXT,
This third line says that we want a column called joketext, which will contain text
(TEXT).

jokedate DATA NOT NULL,

This fourth line defines our last column, called jokedate; this will contain a
date (DATE), which cannot be left blank (NOT NULL).

) DEFAULT CHARACTER SET utf8;


The closing parenthesis ()) marks the end of the list of columns in the table.
DEFAULT CHARACTER SET utf8 tells MySQL that you will be storing UTF-8
encoded text in this table. UTF-8 is the most common encoding used for web
content, Finally, the semicolon tells the mysql client program that you have
finished typing.
Creating a Table (Example) Discussion of commands

 If you typed the above command correctly, MySQL will respond with “Query
OK”, and your first table will be created.
 If you made a typing mistake, MySQL will tell you there was a problem with
the query you typed, and will try to indicate where it had trouble.

Have a look at your new table to make sure it was created properly. Type the
following command:
mysql> SHOW TABLES;
The response should look like this
Creating a Table (Example) Discussion of commands

Let’s take a closer look at the joke table itself using a DESCRIBE command:
mysql> DESCRIBE joke;

For deleting a table use the command below


mysql> DROP TABLE tableName;
Inserting Data into a Table (example)

 Notice that we used double quotes (") to mark where the text of the
joke started and ended. A piece of text enclosed in quotes this way is
called a text string, and this is how you represent most data values in
SQL.

 You will notice, for instance, that the dates are typed as text strings
as well, in the form "YYYY-MM-DD".
Inserting Data into a Table (example)
• If you prefer, you can type text strings surrounded with single
quotes (') instead of double quotes:
mysql> INSERT INTO joke SET
-> joketext = 'Why did the chicken cross the road? To get to the other side!',
-> jokedate = '2009-04-01';

• What happens when the text of a joke itself contains quotes. Well, if
the text contains single quotes, the easiest thing to do is surround it
with double quotes. Conversely, if the text contains double quotes,
surround it with single quotes.
Inserting Data into a Table (example)

• If the text you want to include in your query contains both single
and double quotes,
• You will have to escape the conflicting characters within your text
string.
• You can escape a character in SQL by adding a backslash (\)
immediately before it. This tells MySQL not to interpret the
character as the end of the text string.
Inserting Data into a Table (example)
To make this as clear as possible, here is an INSERT command for a joke
containing both single and double quotes:

mysql> INSERT INTO joke


-> (joketext, jokedate) VALUES ('Knock-knock! Who\'s there? Boo!
"Boo" who? Don\'t cry; it\'s only a joke!', "2009-04-01");

• As you can see, I have marked the start and end of Recall here we have
the text string for the joke text using single quotes. used second
• Therefore I had to escape the three single quotes command to insert
within the string by putting backslashes before data into Table
them.
Inserting Data into a Table (example)

Viewing Stored Data

The SELECT command is used to view data stored in database tables.


This command will list everything that’s stored in the table:

mysql> SELECT * FROM joke;

your results will like below


Inserting Data into a Table
(example)
+----+--------------------------------------------------------------
-+------------+
| id | joketext
| jokedate |
+----+--------------------------------------------------------------
-+------------+
| 1 | Why did the chicken cross the road? To get to the other side!
| 2009-04-01 |
+----+--------------------------------------------------------------
-+------------+
| 2 | 'Knock-knock! Who\'s there? Boo! "Boo" who? Don\'t cry; it\'s only a joke!
| 2009-04-01 |
+----+--------------------------------------------------------------

-+------------+
2 rows in set (0.00 sec)
Inserting Data into a Table
(example)
To View specific Column in Table
If you just want to see any specific column of Table you may use the command like
below.
+----+------------+
mysql> SELECT id, jokedate FROM joke; | id | jokedate |
+----+------------+
| 1 | 2009-04-01 |
This time you will see
+----+------------+
only selected column of +----+------------+
Table as below | 2 | 2009-04-01 |
+----+------------+

2 rows in set (0.00 sec)


Inserting Data into a Table
(example)
To View specific Parts of a Column
Let’s say we wanted to see only the first 20 characters of the joketext
column. Here’s the command we have to use:

mysql> SELECT id, LEFT(joketext, 20), jokedate FROM joke;

+----+----------------------+------------+
| id | LEFT(joketext, 20) | jokedate |
+----+----------------------+------------+
| 1 | Why did the chicken | 2009-04-01 |
+----+----------------------+------------+
+----+----------------------+------------+
| 2 | Knock-knock! Who\'s t | 2009-04-01 |
+----+----------------------+------------+

2 row in set (0.00 sec)


Inserting Data into a Table
(example)
To Count Number of Entries or Rows in Table
If, for example, you wanted to find out how many jokes were stored in your table,
you could use the following command:

mysql> SELECT COUNT(*) FROM joke;


This will result output like below.

+----------+
| COUNT(*) |
+----------+
|2|
+----------+
1 row in set (0.02 sec)

As you can see, you have two jokes in your table.


To Count Specific Number of
Entries or Rows in Table
you can limit your results to include only those database entries that have the specific
attributes you want.

mysql> SELECT COUNT(*) FROM joke WHERE jokedate >= "2009-01-01";


This query will count the number of jokes that have dates greater than or equal to
January 1, 2009.

mysql> SELECT joketext FROM joke WHERE joketext LIKE "%chicken%";

This query displays the full text of all jokes that contain the text “chicken”

To display knock-knock jokes from April 2009 to May 2009, you could use the following
query:
mysql> SELECT joketext FROM joke WHERE
-> joketext LIKE "%knock%" AND
-> jokedate >= "2009-04-01" AND
-> jokedate < "2009-05-01";
Modifying Stored Data

You might like to correct a spelling mistake, or change the date


attached to a joke, such alterations are made using the UPDATE
command.

The general form of the UPDATE command is as follows:

mysql> UPDATE tableName SET


-> colName = newValue, …
-> WHERE conditions;
Modifying Stored Data
So, for example, if we wanted to change the date on the joke we
entered above, we have to use the following command:

mysql> UPDATE joke SET jokedate = "2010-04-01" WHERE id =


"1";

This next command, for example, changes the date of all entries that
contain
the word “chicken”:

mysql> UPDATE joke SET jokedate = "2010-04-01"


-> WHERE joketext LIKE "%chicken%";
Deleting Stored Data

Here’s the command syntax:

mysql> DELETE FROM tableName WHERE conditions;

To delete all chicken jokes from your table, you’d use the following
query:

mysql> DELETE FROM joke WHERE joketext LIKE "%chicken%";


Table 9.1 Useful Comparison Operators for WHERE Clauses
O p e ra to r Name Example D e s c rip tio n
(If Applicable)
= e q u a lit y customerid = 3 T e s ts w h e t h e r t w o v a lu e s a r e e q u a l
> g re a te r th a n a m o u n t > 6 0 . 0 0 T e s ts w h e t h e r o n e v a lu e is g r e a t e r
th a n a n o th e r
< le s s t h a n a m o u n t < 6 0 . 0 0 T e s ts w h e t h e r o n e v a lu e is le s s t h a n
a n o th e r
>= g re a te r th a n o r a m o u n t > = 6 0 . 0 0 T e s t s w h e t h e r o n e v a lu e is
equ al g re a te r th a n o r e q u a l
to a n o th er
<= le s s t h a n o r e q u a l a m o u n t < = 6 0 . 0 0 T e s t s w h e t h e r o n e v a l u e is l e s s t h a n
o r e q u a l to a n o th e r

Useful != or <> not equal quantity != 0 T e s ts w h e t h e r t w o v a lu e s a r e n o t


equal

Comparison IS NOT
NULL
IS NULL
n/a

n/a
address is not T e s ts w h e t h e r fie l d a c t u a lly
null c o n t a in s a v a lu e
address is null T e s ts w h e t h e r fie l d d o e s n o t c o n t a in

Operator for BETWEEN n/a


a v a lu e
amount between T e s ts w h e t h e r a v a lu e is g r e a t e r
t h a n o r e q u a l t o a m in im u m v a lu e
WHRE Clauses
0 and 60.00
a n d le s s t h a n o r e q u a l t o a m a x i-
m u m v a lu e
IN n/a city in T e s ts w h e t h e r a v a lu e is in a
("Carlton", p a r t ic u la r s e t
"Moe")
NOT IN n/a city not in T e s ts w h e t h e r a v a lu e is n o t
("Carlton", in a s e t
"Moe")
LIKE p a tte r n m a tc h name like C h e c k s w h e t h e r a v a lu e m a t c h e s
("Fred %") a p a t t e r n u s in g s i m p le S Q L p a t t e r n
m a t c h in g
NOT LIKE p a tte r n m a tc h name not like C h e c k s w h e t h e r a v a lu e d o e s n ’t
("Fred %") m a tc h a p a tte rn
REGEXP r e g u l a r e x p r e s s io n name regexp C h e c k s w h e t h e r a v a lu e m a t c h e s a
r e g u la r e x p r e s s io n
Summary of Privilege and related
Commands

Types and Levels of Privilege

Three basic types of privileges exist in MySQL:


1. Privileges suitable for granting to regular users
2. Privileges suitable for administrators, and
3. Special privileges.
1. Privileges for Users
Table 8.1 Privileges for Users
P riv ile g e A p p lie s T o D e s c rip tio n
SELECT t a b le s , A llo w s u s e r s t o s e le c t r o w s
c o lu m n s (r e c o r d s ) fr o m t a b le s .
INSERT t a b le s , A llo w s u s e r s t o in s e r t n e w r o w s
c o lu m n s in to t a b le s .
UPDATE t a b le s , A llo w s u s e r s t o m o d ify v a lu e s in
c o lu m n s e x is tin g ta b le r o w s .
DELETE t a b le s A llo w s u s e r s t o d e le t e e x is t in g t a b le r o w s .
INDEX t a b le s A llo w s u s e r s t o c r e a te a n d d r o p in d e x e s o n
p a r t ic u la r t a b le s .
ALTER t a b le s A llo w s u s e r s t o a lt e r t h e s t r u c t u r e o f e x is t in g
ta b le s b y , fo r e x a m p le , a d d in g c o lu m n s ,
r e n a m in g c o lu m n s o r ta b le s , a n d c h a n g in g
d a t a ty p e s o f c o lu m n s .
CREATE d a ta b a se s, A llo w s u s e r s t o c r e a t e n e w
t a b le s d a ta b a s e s o r ta b le s . If a
p a r t ic u la r d a t a b a s e o r ta b le is
s p e c ifie d in th e G R A N T , t h e y c a n
o n ly C R E A T E t h a t d a ta b a s e o r ta b le ,
w h ic h m e a n s th e y w ill
h a v e to D R O P it fir s t .
DROP d a ta b a s e s , A llo w s u s e r s t o d r o p (d e le t e )
t a b le s d a ta b a s e s o r ta b le s .
2. Privileges for Administrators

Table 8.2 Privileges for Administrators


Privilege Description
RELOAD A llo w s a n a d m i n is t r a t o r t o r e lo a d g r a n t t a b le s a n d fl u s h p r iv ile g e s , h o s t s , lo g s ,
a n d ta b le s .
SHUTDOWN A llo w s a n a d m i n i s t r a t o r t o s h u t d o w n t h e M y S Q L s e r v e r .
PROCESS A ll o w s a n a d m in is t r a t o r t o v i e w s e r v e r p r o c e s s e s a n d k il l t h e m .
FILE A llo w s d a t a t o b e r e a d i n t o t a b l e s f r o m f i l e s a n d v i c e v e r s a .
3. Special privileges

Table 8.3 Special Privileges


Privilege Description
ALL G r a n t s a l l t h e p r i v i l e g e s l i s t e d i n T a b le s 8 . 1 a n d 8 . 2 . Y o u c a n a l s o w r i t e A L L
P R I V I LE G E S in s te a d o f A L L .
USAGE G r a n t s n o p r iv ile g e s .T h is w ill c r e a t e a u s e r a n d a llo w h e r t o lo g o n , b u t it
w o n ’t a l l o w h e r t o d o a n y t h i n g . U s u a l l y y o u w i l l g o o n t o a d d m o r e p r i v i l e g e s
la te r .
GRANT & REVOKE Command
The GRANT command is used to give privileges to a user. The opposite of GRANT
is REVOKE. It is used to take privileges away from a user.

The general form of the GRANT command is


GRANT privileges [columns]
ON item
TO user_name [IDENTIFIED BY 'password']
[WITH GRANT OPTION]

The general form of the REVOKE command is


REVOKE privileges [(columns)] ON item
FROM user_name
Examples Using GRANT and REVOKE
To set up an administrator, you can type:

mysql> grant all


-> on *
-> to fred identified by 'mnb123'
-> with grant option;

This grants all privileges on all databases to a user called Fred with the password
mnb123, and allows him to pass on those privileges.

If you don’t want this user in your system, so go ahead and revoke him:

mysql> revoke all


-> on *
-> from fred;
To set up a regular user with no privileges:

mysql> grant usage


-> on books.*
-> to sally identified by 'magic123';
We can give sally the appropriate privileges:
mysql> grant select, insert, update, delete, index, alter, create, drop
-> on books.*
-> to sally;
If we decide to reduce her privileges:
mysql> revoke alter, create, drop
-> on books.*
-> from sally;

And later, when she doesn’t need to use the database any more, we can revoke her
privileges:
mysql> revoke all
-> on books.*
-> from sally;
Example
Further Self Reading

For more information, you can read about setting up a database at the
MySQL online manual at http://www.mysql.com.
https://www.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Begi
nner.html

Assignment:
Write a note about this PPT (not more then two pages).

You might also like