You are on page 1of 30

Server Side Programming

By Dr. Babaousmail Hassen

Lecturer at Binjiang College of NUIST

1
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

2
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;

3
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;
4
Example

5
Read & Understand Following Table Exercises in MySQL
And then Note in your Notebook.
1. Write a SQL statement to create Here is the structure of the table:
a simple table countries including
columns country_id,country_name mysql> DESC countries;
and region_id. +--------------+---------------+------+-----+---------
+-------+
Sample Solution: | Field | Type | Null | Key | Default |
CREATE TABLE countries( Extra |
COUNTRY_ID varchar(2), +--------------+---------------+------+-----+---------
COUNTRY_NAME varchar(40), +-------+
REGION_ID decimal(10,0) | COUNTRY_ID | varchar(2) | YES | |
); NULL | |
| COUNTRY_NAME | varchar(40) | YES | |
The CHAR and VARCHAR types are similar, NULL | |
but differ in in maximum length. | REGION_ID | decimal(10,0) | YES | |
The CHAR and VARCHAR types are declared NULL | |
with a length that indicates the maximum +--------------+---------------+------+-----+---------
number of characters you want to store. For +-------+
example, CHAR(30) can hold up to 30 3 rows in set (0.01 sec)
characters.
The length of a CHAR column is fixed and The length of a VARCHAR column can be a value
can be any value from 0 to 255. from 0 to 65,535. 6
Error
If you will run the previous commands in MySQL, you may get an
error as

NO DATABASE Selected

So before running commands for Table Select the Previously


Created Database of Create a new One

mysql> USE ijdb;

7
Here is the structure of the table:

mysql> DESC dup_countries;


2. Write a SQL statement to create the +--------------+---------------+------+-----+---------
structure of a table dup_countries similar +-------+
to countries. | Field | Type | Null | Key | Default |
Extra |
Sample Solution: +--------------+---------------+------+-----+---------
+-------+
CREATE TABLE IF NOT EXISTS | COUNTRY_ID | varchar(2) | YES | |
dup_countries NULL | |
LIKE countries; | COUNTRY_NAME | varchar(40) | YES |
| NULL | |
Let execute the above code in MySQL | REGION_ID | decimal(10,0) | YES | |
command prompt NULL | |
+--------------+---------------+------+-----+---------
+-------+
3 rows in set (0.03 sec)

8
Here is the structure of the table:

mysql> DESC jobs;


3. Write a SQL statement to create a +------------+--------------+------+-----+---------+-
table named jobs including columns ------+
job_id, job_title, min_salary, max_salary | Field | Type | Null | Key | Default |
and check whether the max_salary Extra |
amount exceeding the upper limit 25000. +------------+--------------+------+-----+---------+-
------+
Sample Solution: | JOB_ID | varchar(10) | NO | | NULL |
|
CREATE TABLE IF NOT EXISTS jobs ( | JOB_TITLE | varchar(35) | NO | | NULL
JOB_ID varchar(10) NOT NULL , | |
JOB_TITLE varchar(35) NOT NULL, | MIN_SALARY | decimal(6,0) | YES | |
MIN_SALARY decimal(6,0), NULL | |
MAX_SALARY decimal(6,0) | MAX_SALARY | decimal(6,0) | YES | |
CHECK(MAX_SALARY<=25000) NULL | |
); +------------+--------------+------+-----+---------+-
------+
4 rows in set (0.16 sec)

9
4. Write a SQL statement to create a table
Here is the structure of the table:
named countries including columns
country_id, country_name and region_id
mysql> DESC countries;
and make sure that no countries except Italy,
+--------------+---------------+------+-----+-----
India and China will be entered in the table.
----+-------+
| Field | Type | Null | Key | Default
Sample Solution:
| Extra |
+--------------+---------------+------+-----+-----
CREATE TABLE IF NOT EXISTS countries (
----+-------+
COUNTRY_ID varchar(2),
| COUNTRY_ID | varchar(2) | YES | |
COUNTRY_NAME varchar(40)
NULL | |
CHECK(COUNTRY_NAME
| COUNTRY_NAME | varchar(40) | YES |
IN('Italy','India','China')) ,
| NULL | |
REGION_ID decimal(10,0)
| REGION_ID | decimal(10,0) | YES | |
);
NULL | |
+--------------+---------------+------+-----+-----
Let execute the above code in MySQL
----+-------+
command prompt
3 rows in set (0.01 sec)

10
5. Write a SQL statement to create a
table named countries including
columns country_id,country_name and
Here is the structure of the table:
region_id and make sure that no mysql> DESC countries;
duplicate data against column +--------------+---------------+------+-----+---
country_id will be allowed at the time of ------+-------+
insertion. | Field | Type | Null | Key |
Default | Extra |
Sample Solution: +--------------+---------------+------+-----+---
------+-------+
| COUNTRY_ID | varchar(2) | YES |
CREATE TABLE IF NOT EXISTS
| NULL | |
countries (
| COUNTRY_NAME | varchar(40) | YES
COUNTRY_ID varchar(2) NOT NULL, | | NULL | |
COUNTRY_NAME varchar(40) NOT | REGION_ID | decimal(10,0) | YES |
NULL, | NULL | |
REGION_ID decimal(10,0) NOT NULL, +--------------+---------------+------+-----+---
UNIQUE(COUNTRY_ID) ------+-------+
); 3 rows in set (0.01 sec)

Let execute the above code in MySQL


command prompt
11
Practice now: write code in your notebook to create following Table
in MySQL

12
Revision
Some Simple Questions

13
Which Command is used to
see all databases in MySQL?

14
Which command is used to
delete any database in
MySQL?

15
What is the command line to
create any database in
MySQL?

16
What is the meaning of id INT
NOT NULL
AUTO_INCREMENT
PRIMARY KEY?

17
Which command is used to see
the structure of tables in
MySQL?

18
Which command is used to see
the data of a table in MySQL?

19
Which command is used to
insert data in a table in
MySQL?

20
When we get this error “NO
DATABASE Selected “

21
What is the meaning of Grant
and Revoke Commands?

22
Your First PHP Script
Open your favorite text editor or HTML editor and create a new file called
today.php. Type this into the file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Today&rsquo;s Date</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<p>Today&rsquo;s date (according to this web server) is
<?php
echo date('l, F dS Y.');
Most of this is plain HTML; however, the line
?> between <?php and ?> is PHP code. <?php
</p> marks the start of an embedded PHP script and
</body> </html> ?> marks the end of such a script
23
Save the file as today.php.

Alert Windows users should note that, to save a file with a .php extension in
Notepad, you’ll need to select All Files as the otherwise, Notepad will
unhelpfully save the file as today.php.txt, which will fail to work.

If you are getting any problem in editing PHP scripts. There are a number of solid
text editors and Integrated Development Environments (IDEs) with rich support for
editing PHP scripts that you can download for free. Here are a few that work on
Windows, Mac OS X, and Linux:

NetBeans: http://www.netbeans.org/features/php
Aptana: http://www.aptana.com/php
Komodo Edit: http://www.activestate.com/komodo_edit

Save the file in Proper Directory

If you’re using an Apache server you installed manually, the web root directory is the
htdocs directory within your Apache installation (that is, C:\Program Files\Apache
Software Foundation\Apache2.2\htdocs on Windows
24
Alert Use encoding UTF 8 while saving the earlier file

Sometime we receive a message from the window that we cannot save the file in the
earlier directory. In that case just save the file on desktop and then cut and paste in
the previous directory

Type the following address in address of your browser- http://localhost/today.php or


http://localhost:port/today.php and

You will see the following out put if everything is correct otherwise you may get an
error message due to some mistakes during the installation.

Today’s date (according to this web server) is Sunday, September 29th 2019.

25
The web server interpret everything between delimiters, and convert it to regular
HTML code before it sends the requesting browser.

The browser is presented with the following if you will see the source of following
link:

Today’s date (according to this web server) is Wednesday, November 15th 2017.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Today&rsquo;s Date</title>
<meta http-equiv="content-type“ content="text/html; charset=utf-8"/>
</head>
<body>
<p>Today & rsquo;s Date (according to this web server) is Wednesday, April 1st
2009.
</p>
</body>
</html>
26
If you use the View Source feature in your browser, all you’ll see is a regular HTML
file with the date in it. The PHP code (everything between <?php and ?> in the
code above) was interpreted by the web server and converted to normal text
before it was sent to your browser.

• In the above example, we placed the date, according to the web server, into the
web page. If we had inserted the date using JavaScript, we will only be able to
display the date according to the computer on which the web browser was
running.

• Java Script can delay the display of a webpage on slower computers significantly,
as the browser must run the script before it can display the web page.

27
Another Simple Example of PHP
Script
To process the form, we need to create the script say processorder.php. Open
your text editor and create this file. Type in the following code:

<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
</body>
</html>

Notice, how everything we’ve typed so far is just plain HTML. It’s now
time to add some simple PHP code to our script. 28
Embedding PHP in HTML
Under the <h2> heading in your file, add the following lines:
<?php
echo '<p>Order processed.</p>';
?>
Save the file and load it in your browser (Type the following address in
address of your browser- http://localhost/processorder.php or
http://localhost:port/processorder.php). You should see something similar
to the output shown in Figure 1.2.

29
Home Assignment-III

 Prepare a brief note about “Introduction of


PHP and some basic operations”
send it to this e-mail: baw.hassan@outlook.com
Deadline: before next Tuesday class.

30

You might also like