PDF Unit III Mysql Advanced & PHP Basic
PDF Unit III Mysql Advanced & PHP Basic
PDF Unit III Mysql Advanced & PHP Basic
AGGREGATE FUNCTIONS
1
SELECT MAX(percentage)AS highestpercentage FROM
studentpersonal;
Syntax
SELECT MIN(column_name) FROM table_name
MATHEMATICAL FUNCTIONS
1.The ABS() : returns the absolute value of a number.
ABS(X) Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
In the above ex. It gives the absolute value of 2 is
2. It does not show any effect on positive numbers,
where else -32 the absolute value is 32.
2. The CEILING().Ceil() takes the number and rounds it to
the nearest integer above its current value.
Eg.
Select CEIL(11.256)
O/P
12
In the above ex. The value 11.256 is round to 12. It round
to the nearest integer above its current value.
2
In the above ex. The value 11.256 is round to 11. It round
to the nearest integer below its current value.
Round (11.256,2)
O/P
11.26
In the above ex. The value 11.256 is round to 11.26. It
round to the nearest integer above its current value.
MYSQL>select SQRT(9);
O/P
3
STRING FUNCTIONS
>Select upper(‘computer’);
o/p
4
COMPUTER
In the above example the upper function convert the string
given in lower case to upper case as shown in the ouput.
Upper function have no effect if the string given in upper
case.
3. The LOWER() function converts characters into lower-
case letters.
>Select lower(‘computer’);
o/p - computer
SYNTAX
LEFT(string, number_of_chars)
>Select left(‘computer’,4);
o/p
comp
SYNTAX
RIGHT(string, number_of_chars)
>Select right(‘computer’,3);
o/p
ter
5
O/P
0
CONCAT(‘TWELVE’,’CT’);
O/P
TWELVE CT
6
12. REVERSE(str) : Returns the string str with the order
of the characters reversed.
14. RTRIM(str)
TRIM(string)
o/p
Example.com
7
DATE & TIME FUNCTIONS
>SELECT NOW();
>SELECT CURTIME(),CURDATE();
O/P
59
O/P
22-01-2020
DAYOFMONTH(date)
8
Date can be given to any of the function either fix
date, curdate or field name. Any one way.
DAYOFWEEK(date)
o/p
4
SELECT DAYNAME(CURDATE());
DAYNAME(‘2020-05-13’);
FRIDAY
YEAR('2020/01/23'),
2020
SELECT DAY(CURDATE());
13
• MONTH(date)
• MONTHNAME(date)
9
mysql> SELECT DAYOFYEAR('2021-01-01');
-> 34
• HOUR(time)
• MINUTE(time)
PERIOD_ADD(P,N)
PERIOD_DIFF(P1,P2)
-> 3
10
TO_DAYS(date)
Triggers in SQL
Triggers Views
11
A view is considered a database object although the view takes up
no storage space on its own.
12
INTRODUCTION to PHP BASICS
History of PHP
PHP began in 1995 when Rasmus Lerdorf developed a Perl/CGI
script toolset he called the Personal Home Page or PHP
PHP 2 released 1997 (PHP now stands for Hypertext
Processor). Lerdorf developed it further, using C instead
What is PHP?
PHP == ‘Hypertext Preprocessor’
Open-source, server-side scripting language
Used to generate dynamic web-pages
PHP scripts reside between reserved PHP tags
This allows the programmer to embed PHP scripts within HTML
pages
PHP files can contain text, HTML tags and scripts
PHP files have a file extension of ".php", ".php3", or
".phtml"
PHP is compatible with almost all servers used today
(Apache, IIS (Internet Information Server ), etc.)
A PHP script always starts with <?php and ends with ?>. A
PHP script can be placed anywhere in the document. Once
the program is written, it should be saved under the www
directory under WAMP. first script PHP script can be typed
in any text editor like notepad++. Each code line in PHP
must end with a semicolon. The semicolon is a separator and
is used to distinguish one set of instructions from
another. The extension to php document should be always
.Php after the document is saved you should be able to
access it via your browser if there are no errors the
output will be as follows.
Format :
<?php
?>
Eg:-
<?php
Print ”Hello web”;
?>
13
There are two basic statements to output text with PHP:
echo and print.
Print () function
In PHP there are two basic ways to get output: echo and print.
<HTML>
<HEAD>
<TITLE>A PHP script including HTML </TILE>
</HEAD>
<BODY>
<b>
<?php
Print “Hello Web”;
echo “Hello world”;
?>
</BODY>
</HTML>
14
Comments in PHP
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
PHP Variables
Dynamic Variable:-
15
• When you assign a text value to a variable, put quotes
around the value.
• In PHP, a variable does not need to be declared before
adding a value to it.
<?php
$txt="Hello World!";
$x=16;
?>
Escape character
Data Types
Assignment Operators
16
The basic assignment operator in PHP is "=". It means that
the left operand gets set to the value of the expression on
the right. That is, the value of "$x = 5" is 5.
Incrementing/Decrementing Operators
Comparison Operators
Logical Operators
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
18
Hello World! What a nice day!
<?php
echo strlen("Hello world!");
?>
12
<?php
echo substr("Hello world!",6,5);
?>
19
PHP strrev() Function
Syntax : strrev(string)
Eg.
<?php
?>
!dlroW olleH
Conditional Statements
The if Statement
<html>
<body>
20
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
?>
</body>
</html>
if ( expression )
{
// code to execute if the expression evaluates to true
}
else
{
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
21
</body>
</html>
Syntax
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1
and label2;
}
22
Ex.
<html>
<body>
<?php
$x=1;
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
LOOPS
Loop statements are designed to enable you to achieve
repetitive tasks. Almost without exception, a loop
continues to operate until a condition is achieved, or you
explicitly choose to exit the loop.
while ( expression )
{
// do something
}
As long as a while statement's expression evaluates to
true, the code block is executed over and over again till
the expression becomes false. Within the block, you usually
23
change to either increment /decrement the value something
that affects the while statement's expression; otherwise,
your loop continues indefinitely.
The for loop is used when you know in advance how many
times the script should run.
The example below defines a loop that starts with i=1. The
loop will continue to run as long as i is less than, or
equal to 5. i will increase by 1 each time the loop runs:
24
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
Output:The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
What Is a Function?
A function is a block of code that is not immediately
executed but can be called by your scripts when needed.
Functions can be built-in or user-defined. They can require
information to be passed to them and usually return a
value.
Syntax
Function functionName()
{
code to be executed;
}
Defining a Function
You can define a function using the function statement:
25
function some_function( $argument1, $argument2 )
{
// function code here
}
The name of the function follows the function statement and
precedes a set of parentheses. If your function is to
require arguments, you must place comma-separated variable
names within the parentheses. These variables will be
filled by the values passed to your function. If your
function requires no arguments,you must nevertheless supply
the parentheses.
Eg:-
<?php
function bighello ()
{
Print “<h1> Hello </h1>”;
}
?>
What Is an Array?
An array allows you to store as many values as you want in
the same variable. Each value is indexed within the array
by a number or a string. An array is flexible. It can store
two values or two hundred values without the need to define
further variables. Each element can be accessed directly
via its index.
You can sort items numerically, alphabetically, or even
according to a system of your own.
An index to an array element can be either a number or a
string. By default, array elements are indexed by number
starting at zero. Array is always one less than the number
of elements the array contains.
Accessing Arrays
You can access an element of an array by using its index:
print $user[4]
Numeric Arrays
$cars=array("Saab","Volvo","BMW","Toyota");
$cars[0]="Swift";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
Example
27
<?php
$cars[0]="Swift";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are cars models.";
?>
<?php
$ages['Peter'] = "32";
$ages['Joy'] = "30";
$ages['Joe'] = "34";
28
define a numerically indexed array and use count() to
access its last element:
29
Manipulating Arrays
Joining Two Arrays with array_merge()
array_merge() accepts two or more arrays and returns a
merged array combining
all their elements. Ex. we create two arrays, joining the
second to the first, and loop through the resultant third
array:
$first = array("a", "b", "c");
$second = array(1,2,3);
$third = array_merge( $first, $second );
foreach ( $third as $val )
{
print "$val<BR>";
}
The $third array contains copies of all the elements of
both the $first and $second
arrays. The foreach statement prints this combined array (
'a', 'b', 'c', 1, 2, 3 ) to the
browser with a <BR> tag between each element
30
browser. The $first array now contains its original
elements as well the three
integers we passed to the array_push() function, all of
these are printed to the
browser within the foreach statement.
<?php
$an_array = array("a", "b", "c");
while ( count( $an_array) )
{
$val = array_shift( $an_array);
print "$val<BR>";
print "there are ".count($an_array)." elements in
\$an_array <br>";
}
?>
Using array_shift() to remove and print every element in an
array.
array_shift() is useful when you need to create a queue and
act on it until the queue
is empty.
Sorting Arrays
This section introduces some functions that allow you to
sort both numerically
indexed and associative arrays.
$an_array = array("x","a","f","c");
sort( $an_array);
foreach ( $an_array as $var )
31
{
print "$var<BR>";
}
32
33