You are on page 1of 17

MODULE 5

Subtopic 1: GLOBAL VARIABLES

SUPERGLOBAL VARIABLES

 $_SERVER
 is an array containing information such as headers, paths, and script locations.
 entries were created by the web server.
 Index ‘PHP_SELF’ contains the filename of the currently executing script.
 $_GET
 an associative array variables passed to the current script via the URL parameters.
 $_POST
 an associative array of variables passed to the current script via the HTTP POST method.
 $_REQUEST
 an associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE
 $_COOKIE
 an associative array of variables passed to the current script via HTTP Cookies
 $_SESSION
 an associative array containing session variables available to the script
 $_ENV
 an associative array of variables passed to the current script via the environment method
Example

Output: before button was clicked Output: submit get button was clicked
Output: submit post button was clicked

COOKIES

 are mechanism for storing data in the remote browser and thus tracking or identifying return
users.
 small amount of information containing variable=value pair (user’s computer).
 users can refuse to accepts cookies.
 Managing cookies can be done using setcookie() function
 syntax: setcookie()
bool setcookie ( string $name [, string $value [, int
$expire = 0 [, string $path [, string $domain [, bool
$secure = false [, bool $httponly = false ]]]]]] )

Example: PHPSetCookies.php Output 1: cookies were set

Example: PHPDisplayCookies.php Output 2: after 10 secs

Example: PHPDeleteCookies.php Output 3: delete cookies


Subtopic 2: PHP SESSION
SESSION

 are mechanism for storing data on the server itself.


 is the time that a user spends at your Web site.
 more secure than cookies and can store much more information
 to open a session use session_start() function
 always set at the beginning of each Web page.
 to close the session use session_destroy() function
 gets rid of all the session variable information that’s stored in the session file.
 the statement does not affect the variables set on the current page.
 to unset session variables use unset() function
syntax
unset($_SESSION[‘varname’]);
Example: PHPSetSession.php

Example: PHPUnsetSession.php

Example: PHPDisplaySession.php

Example: PHPDeleteSession.php

Output: user load PHPDisplaySession.php page


Ouput: user clicked the Login link

Out: user cicked the email link

Output: user clicked the logout link

REGULAR EXPRESSION

 were used to efficiently search for patterns in a given text.


 also known as regex or regexp.
 PHP implements Percl Compatible Regular Expression (PCRE)
 PCRE function starts with preg_
 preg_match() function
 Performs a regular expression match
 Syntax: int preg_match ( string $pattern , string
$subject [, array &$matches [, int $flags = 0 [, int
$offset = 0 ]]] )
 Regex Meta characters

Symbol Description

^ Marks the start of a string


$ Marks the end of a string
. Matches any single character
| Boolean OR
() Group elements
[abc] Item range (a,b or c)
[^abc] Not in range (every character except a,b, or
c)
\s white-space character
a? Zero or one ‘a’ character. Equals to a{0,1}
a* Zero or more of ‘a’
a+ One or more of ‘a’
a{2} Exactly two of ‘a’
a{,5} Up to five of ‘a’
a{5,10} Between five to ten of ‘a’
\w Any alpha numeric character plus
underscore. Equals to [A-Za-z0-9_]
\W Any non alpha numeric characters
\s Any white-space character
\S Any non white-space character
\d Any digits. Equal to [0-9]
\D Any non-digits. Equal to [^0-9]
i Ignore Case
m Multiline Mode
S Extra analysis of pattern
u Pattern is treated as UTF-8

Example

Example Description
‘/hello/’ It will match the word hello
‘/^hello/’ It will match hello at the start of a string.
Possible matches are hello or helloworld,
but not worldhello
‘/hello$/’ It will match hello at the end of a string.
‘/he.o/’ It will match any character between he and
o. Possible matches are helo or heyo, but
not hello
‘/he?llo/’ It will match either llo or hello
‘/hello+/’ It will match hello on or more time. E.g.
hello or hellohello
‘/he*llo/’ Matches llo, hello or hehello, but not
hellooo
‘/hello|world/’ It will either match the word hello or world
‘/(A-Z)/’ Using it with the hyphen character, this
pattern will match every uppercase
character from A to Z. E.g. A, B, C…
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the
characters ab. E.g. matchesabc, but not
abcc
‘/abc{1,}/’ Matches one or more c character after the
characters ab. E.g. matches abcor abcc
‘/abc{2,4}/’ Matches between two and four c character
after the characters ab. E.g. matches abcc,
abccc or abcccc, but not abc

 Useful Regex Function


Email validation

MODULE 6
Subtopic 1: DATABASE
INTODUCTION TO DATABASES

 Database - is an ordered collection of information from which a computer program can quickly
access.
 Information stored in computer databases is actually stored in tables similar to spreadsheet.
 A record in a database is a single complete set of related information.
 Fields - are the individual categories of information stored in a record.

Example: Employee Directory DB


Fields

LAST_NAME FIRST_NAME ADDRESS CITY STATE ZIP


BLAIR DENNIS 204 Spruce MA 01506
Lane
HERNANDE LOUIS 68 Boston Post MA 01562
Z Road
MILLER ERICA 271 Baker Hill MA 01515
Road
MORINAGA SCOTT 17 Ashley MA 01515
Road
PICARD RAYMOND 1113 Oakham MA 01531
Road
Records (Row)
Also called as flat-file database that stores information in a single table.

RELATIONAL DATABASE

 Stores information across multiple related tables.


 Composed of Primary Table and Related Table (child table)
 Primary Key - is a field that contains a unique identifier for each record in a primary table.
 Foreign Key - is a field in a related table that refers to the primary key in a primary table.
 Normalization - is the process of breaking tables into multiple related tables to reduce
redundant information.
THE MYSQL DATABSE

 One important aspect of database management is its querying capability


 A query is a structured set of instructions and criteria for retrieving, adding, modifying, and
deleting database information.
 Data Manipulation Language (DML) - is use for creating queries.
 Structured Query Language (SQL) - has a standard data manipulation language among many
database management system.

OPEN SOURCE DATABASE

 Either as outgrowths of earlier academic research (PostgreSQL)


 Developments of lightweight storage tools for websites (MySQL)
 Open-sourced commercial products (InterBase)

 Most popular open source relational database


 An Australian academician named David Hughes (Hughes Technologies) wrote a very
lightweight database engine called mSQL (short for mini SQL) -- it didn't implement a
number of features required for full ANSI SQL certification, but it was very small and very
fast.
 mSQL was distributed as shareware
 incapable of doing a number of essential things -- like joins

A BIT HISTORY

 improvement was done by a Swedish programmer Monty Widenius


 MySQL rapidly grew until, although it's still a fast, light database, it's also a pretty powerful one.
 MySQL is written in C++, compiles using GNU gcc, and is pretty portable -- it has been ported to
OS/2, Windows 95 and NT, as well as a variety of flavours of UNIX

FEATURES OF SQL

 According to the article of Ian Gilfillan in 2004, the following are the claimed features of MySQL:
 High availability
 High scalability
 High performance

CONNECTING TO MYSQL: CONSOLE BASE

 Console Base

mysql >
 Go to the Command Window
Start\Run
Type command or cmd
 Go to the MySQL path
C:\xampp\mysql\bin\mysql –u root –p
C:\xampp\mysql\bin\mysql –u root
**Enter password if there is any.
Type commands in MySQL prompt which looks like this:

mysql >

MYSQL LOG IN

MYSQL PROMPTS
PROMPT MEANING
mysql> Ready for a new command
-> Waiting for the next line of multiple line command.
‘> Waiting for next line, waiting for completion of a string that began with single
quote (‘ ‘ ‘).
“> Waiting for next line, waiting for completion of a string that began in double quote
(‘ “ ‘).
`> Waiting for next line, waiting for completion of an identifier that began with a
backtick (‘ ` ‘).
/*> Waiting for next line, waiting for completion of a comment that began with /*.

BASIC COMMANDS

 To clear command:

mysql > \c
 Get status from the server:

mysql > status

mysql > \s
 Need help?:

mysql > \h

mysql > ?

mysql > \?

mysql > help


 To display the SQL version:

mysql > select version();

EXAMPLE
BASIC COMMANDS

 Display current user:

mysql > select user();


 Display current date:

mysql > select current_date()


 Display today’s date and time:

mysql > select now()

Example
BASIC COMMANDS

 To quit

mysql > \q

mysql > quit

mysql > exit

Subtopic 2: MYSQL COMMANDS


MYSQL

Data manipulation Language (DML)

Create, Select, Insert, Update, Delete

DATABASE CREATION

 Syntax:
CREATE DATABASE ;

mysql> CREATE DATABASE DBStudent;


Showing and Using the Database:

mysql> SHOW databases;


Syntax: USE ;

mysql> USE DBStudent;


Note: It should display Query OK, 1 row affected as a result.

Example:

TABLE CREATION
 Syntax
CREATE TABLE <table name> (field name field type(size) condition...);

mysql> CREATE TABLE tblStudent(id int(5) primary key


not null auto_increment, name char(25))
Display the structure of the table:
Syntax: DESCRIBE <table name>;

mysql > DESCRIBE tblStudent


Displaying the list of table:

mysql> SHOW tables;

SAMPLE SCREEN

ADDING OF PRIMARY KEY

mysql> create table primaryTable (id int not null auto_increment, name varchar(30), primary
key (id));

mysql> create table keyTable (id int not null auto_increment, name varchar(30), key (id));

mysql> create table uniqueTable (id int not null auto_increment, name varchar(30), unique key
(id));

FIELD DATA TYPES


TYPES RANGE STORAGE
BOOL -128 to 127; 0 is considered false
INT OR INTEGER -2147483648 to 2147483647
FLOAT A small number with a floating decimal point.
DOUBLE A large number with a floating decimal point
DATE YYYY-MM-DD
TIME HH:MM:SS
CHAR(M) Fixed length string between 0 to 255 characters
VARCHAR(M) Variable length string between 1 to 65,535 characters
THE ‘SHOW’ COMMAND

This time you can show additional information of a table

mysql > SHOW COLUMNS FROM tblStudent;

mysql > SHOW TABLE STATUS;

mysql > SHOW TABLE STATUS \G;

mysql > SHOW TABLE STATUS LIKE ‘tblStudent’ \G;

mysql > SHOW CREATE TABLE tblStudent \G;

Example
MODIFYING THE TABLE STRUCTURE

 Adding new column


Syntax: ALTER TABLE <table name> ADD <column name> <data type(size)>;

mysql > ALTER TABLE tblStudent ADD gender char(1);

 Changing Field Type Sizes


Syntax: ALTER TABLE <table name> MODIFY <column name> <data type(size)>;

mysql > ALTER TABLE tblStudent MODIFY gender char(7);

 Changing Field Names


Syntax: ALTER TABLE <table name> CHANGE <column name> <new column name> <data
type(size)>;

mysql > ALTER TABLE tblStudent CHANGE gender sex char(7)


 Dropping column
Syntax: ALTER TABLE <table name> DROP <column name>;

mysql > ALTER TABLE tblStudent DROP sex;

INSERTING VALUES TO YOUR TABLE


 Syntax: INSERT INTO <table name> VALUES (value1, value2, value3, ....);

mysql > INSERT INTO tblStudent VALUES (10001, ‘Juan Dela Cruz’);
To view use ‘SELECT’ command:
Syntax: SELECT * FROM <table name>;

mysql> SELECT * FROM tblStudent

Example:

INSERTING VALUES IN A SPECIFIC COLUMN


 Syntax: INSERT INTO <table name> (column name) VALUES (value);

mysql> INSERT INTO tblStudent (name) VALUES (‘Ma. Conchita Borromeo’);


OTHER STRUCTURE

mysql> INSERT INTO tblStudent (id,name) VALUES (10003,’Mar Roxas’);

mysql> INSERT INTO tblStudent (name,id) VALUES (’Jojo Binay’,10004);

UPDATING THE RECORDS


 Syntax: UPDATE <table name> SET <column name> = <value> WHERE <column name> =
<value>;

mysql> UPDATE tblStudent SET name=‘Ma. Conchita Dimagiba’ WHERE id=‘10002’;

UPDATING MULTIPLE COLUMNS


 Syntax: UPDATE <table name> SET <column name> = <value>, <column name> = <value>
WHERE <column name> = <value>;

mysql> UPDATE tblStudent SET name=‘Juan Dela Peña’, gender=‘M’ WHERE id=‘10001’;

DELETING OF COLUMNS
 Syntax: DELETE FROM <table name> WHERE <column name> = <value>;

mysql> DELETE FROM tblStudent WHERE id=‘10002’;


DELETING ALL DATA IN A TABLE
 Syntax: TRUNCATE TABLE <table name>
Or

mysql> TRUNCATE TABLE tblStudent;

mysql> DELETE FROM tblStudent;

TO DELETE A TABLE
 Syntax: DROP TABLE <table name>;

mysql> DROP TABLE tblStudent;


*You remove a table from the database when it is not required. You use the DROP TABLE
statement to remove a table. When you remove a table, all the data from the table is also
deleted.

TO DELETE DATABASE
 Syntax: DROP DATABASE <database name>;

mysql> DROP DATABASE dbStudent;

You might also like