2. •Functions in PHP
•PHP Form
•Post And Get Method
•Usage of PHPMYADMIN
•Making Database
•MYSQL
•PHP data base connectivity
3. Functions are the group of statements that you can execute as a
single unit. Function definations are the lines of code that make up a
function
The syntax for defining a function is:
<?php
function name function(parameters) {
statements;}
?>
There are two types of functions.
1. Built-in functions.
2. User defined functions.
4. PHP has lots of built-in functions that we use all the time.
Some of the function are given below:
•PHP Array Functions
•PHP Calendar Functions
•PHP File System Functions
•PHP MySQL Functions
•Math functions
5. These functions allow you to interact with and manipulate arrays in
various ways. Arrays are essential for storing, managing, and operating on
sets of variables. Some Array Functions are:
Array(), Array push(), Array pop() etc.
<html>
<body>
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
7. The calendar extension presents a series of functions to simplify converting
between different calendar formats. Some PHP Calendar Functions are:
Cal info(), Cal days in month() etc.
<html>
<body>
<?php
print_r(cal_info(0));
?>
</body>
</html>
9. The filesystem functions are used to access and manipulate the filesystem
PHP provides you all the posible functions you may need to manipulate a
file.
List:
Copy(), delete(), file(), filetype() etc
10. These are functions dealing with MySQL handling and logging or to control
the MySQL through PHP functions.
List:
mysql_close(), mysql_connect() etc.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
11. The math functions can handle values within the range of integer and
float types.
List:
hexdec(), sqrt(), sin() etc
<?php
echo hexdec("1e") . "<br>";
echo hexdec("a") . "<br>";
echo hexdec("11ff") . "<br>";
echo hexdec("cceeff");
?>
13. A user defined function is a user-defined set of commands that
are carried out when the function is called.
Function functionName()
{
code to be executed;
}
•A function should start with keyword function and all the function code
should be put inside { and } brace.
•A function name can start with a letter or underscore not a number.
•The function names are case-insensitive.
14. •Information can be passed to functions through arguments. An argument
is just like a variable.
•Arguments are specified after the function name, inside the parentheses.
•You can add as many arguments as you want, just separate them with a
comma.
15. <?php
function say_hello() {
echo “<p>Hello everybody!</p>”;
}
say_hello();
Function writeMSg() {
Echo “How are You?”;
}
writeMsg();
?>
17. <?php
function familyName($fname,$year)
{
echo “Name of Person is $fname. Born in $year <br>";
}
familyName(“Zeeshan Ahmed","1993");
familyName(“Abdul wahab","1992");
familyName(“Rashid Nawaz","1993");
familyName(“Saad Sattar","1991")
?>
18. If we call the function setHeight() without arguments it takes
the default value as argument:
<?php
function setHeight($minheight=50)
{
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
19. When you create a form you have two choices for the METHOD
attribute. You can use one of the following two methods to pass
information between pages.
• GET method
• POST method
They both pass the data entered into the form along with the
form field name to the web server.
20. Information sent from a form with the GET method is visible to
everyone (all variable names and values are displayed in the URL). GET also
has limits on the amount of information to send. The limitation is about
2000 characters. However, because the variables are displayed in the URL,
it is possible to bookmark the page. This can be useful in some cases.
GET should NEVER be used for sending passwords or other sensitive
information!
22. Information sent from a form with the POST method is invisible to
others (all names/values are embedded within the body of the
HTTP request) and has no limits on the amount of information to
send.
However, because the variables are not displayed in the URL, it is
not possible to bookmark the page.
Developers prefer POST for sending form data.
24. Both GET and POST create an array (e.g. array( key => value, key2 =>
value2, key3 => value3, ...)). This array holds key/value pairs, where keys
are the names of the form controls and values are the input data from
the user.
Both GET and POST are treated as $_GET and $_POST. These are
superglobals, which means that they are always accessible, regardless of
scope - and you can access them from any function, class or file without
having to do anything special.
$_GET is an array of variables passed to the current script via the URL
parameters.
$_POST is an array of variables passed to the current script via the HTTP
POST method.
25. •PhpMyAdmin is a handy, graphical administration tool written in php for
creating and managing MySQL databases , using a web user interface.
•The interface is straight-forward and easy to learn and it allows users to
execute SQL queries manually.
•It is also open source, so you can download and use it for free.
26. •Create, drop, browse and modify databases.
•Perform maintenance on databases.
•Run query operations, drop, create, update, check, repair tables and
more.
•Manage MySQL users and privileges.
27.
28. You can access phpMyAdmin directly visiting the following URL;
http://localhost/phpMyAdmin
Or your server ip address
http://127.0.01/phpmyadmin
29.
30.
31.
32.
33.
34. MySQL also called "My Sequel" is the world's second most widely
used open-source relational database management system (RDBMS).
MySQL is a relational database management system (RDBMS), with
no GUI tools to administer MySQL databases or manage data contained
within the databases.
Users may use the included command line tools, or use MySQL "front-
ends", desktop software and web applications that create and manage
MySQL databases, build database structures, back up data, and work
with data records.
The official set of MySQL front-end tools, MySQL Workbench is actively
developed by Oracle, and is freely available for use.
35. The official MySQlworkbench is a free integrated environment
developed by MySQL , that enables users to graphically administer
MySQL databases and visually design database structures. MySQL
Workbench replaces the previous package of software, MYSQL GUI
Table. Similar to other third-party packages, but still considered the
authoritative MySQL front end, MySQL Workbench lets users
manage database design & modeling, SQL development (replacing
MySQL Query Browser) and Database administration (replacing
MySQL Administrator).
MySQL Workbench is available in two editions, the regular Free and
open source Community Edition which may be downloaded from
the MySQL website, and the proprietary Standard Edition which
extends and improves the feature set of the Community Edition.
36. The create user statement will create a new Mysql user account. But it have no
privileges. To create user you must have create user privilege or insert privilege for
mysql Database.
Create user ‘username’@’servername’ identified by ‘Password’;
The Drop user statement will drop one or more user accounts and their
privileges.to use this statement you must have global create privileges or delete
privilege for the mysql database.
Drop user ‘username’@’servername’;
37. The Grant statement grant privileges to the Mysql user account.to use grant
you must have the grant privilege or the privilege that you are granting.
Note: if the username describe in the grant option does not already exist.grant
may create it under the describes conditions.
Grant all on * . * to ‘username’@’servername’ ;
Grant select on * . * to ‘username’@’servername’ ;
Grant select on DB.* to ‘username’@’servername’ ;
Grant select, insert on DB.TB1 to ‘username’@’servername’ ;
Grant insert (c1,c2) on DB.TB1 to ‘username’@’servername’ ;
38. The Rename user statement rename the existing Mysql accounts.to use it you
must have the global create privilege or update privilege for Mysql Database.
An error occurs if any old account does not exist or any new account exists.
Rename cause the privileges held by the old user to be those held by new user.
Rename user does not drop or invalidate the databases or object s within them
created by the old user
Rename user ‘Existing name’@’servername’ to ‘new name’@’servername’;
39. The Revoke statement enable administrators to revoke
privileges from MySQL users.
To use the revoke statement you must have the grant
option and the privilege that you are revoking
Revoke all privileges, grant option from ‘user’@
’servername’;
Revoke select on * . * from ‘user’@ ’servername’;
40. Set Password statement is used to assign password to the existing
Mysql users
If the password is specified using the password function the password is given to the
function as argument,wwhich hash the password and return the encrypted password
41. SQL stands for Structured Query Language.
It is the most commonly used relational database language today.
SQL works with a variety of different programming languages, such
as Visual Basic.
Includes data definition language (DDL), statements that specify
and modify database schemas.
Includes a data manipulation language (DML), statements that
manipulate database content.
SQL data is case-sensitive, SQL commands are not.
42. SQL Must be embedded in a programming language, or used with a
Programming like VB
SQL is a free form language so there is no limit to the the number of
words per line or fixed line break.
Syntax statements, words or phrases are always in lower case; keywords
are in uppercase.
Not all versions are case sensitive!
43. CREATE TABLE: used to create a table.
ALTER TABLE: modifies a table after it was created.
DROP TABLE: removes a table from a database.
INSERT: adds new rows to a table.
UPDATE: modifies one or more attributes.
DELETE: deletes one or more rows from a table.
44. Things to consider before you create your table are:
The type of data.
The table name.
What column(s) will make up the primary key.
The names of the columns.
CREATE TABLE <table name>
( field1 datatype ( NOT NULL ), key type(optional)
field2 datatype ( NOT NULL ) );
45. To add or drop columns on existing tables.
ALTER TABLE <table name>
ADD attribute data type;
Or Modify attribute data type;
or
DROP COLUMN attribute;
46. Drop Table statement Has two options:
Specifies that any foreign key constraint violations that are
caused by dropping the table will cause the corresponding rows of the
related table to be deleted.
blocks the deletion of the table of any foreign key constraint
violations would be created.
DROP TABLE <table name> [ RESTRICT|CASCADE ];
47. Create table Departments
(id int not NULL primary key auto_increment,
Department char(15));
ALTER table Departments
Add batch varchar (20);
ALTER table Departments
Drop Column batch;
Drop TABLE Departments;
48. To insert a row into a table, it is necessary to have a value for
each attribute, and order matters.
Insert into Table(Attr1, Attr1, Attr1)values(NULL,’value1’,’value2’);
Insert into Departments(id, Department, Batch) values(NULL,’IT’,’5’);
49. To update the content of the table:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
update Departments SET Batch=20 WHERE id=‘2’
Departments=‘physics’;
50. To delete rows from the table:
DELETE FROM <table name>
WHERE <condition>;
DELETE FROM Departments WHERE id=‘2’;
51. A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
Specifies the
attributes that
are part of the
resulting relation
Specifies the
tables that serve
as the input to the
statement
Specifies the
selection condition,
including the join
condition.
52. Using a * in a select statement indicates that every attribute of the
input table is to be selected.
Example: SELECT * FROM Departments;
To get unique rows, type the keyword DISTINCT after SELECT.
Example: SELECT DISTINCT batch from Departments
53. Where clause is used to retrieve data from the table conditionally.it can
appear only after FROM clause.
Select Column From Table Where Condition;
Select * From Departments Where Batch=‘5’;
54. A join can be specified in the FROM clause which list the two
input relations and the WHERE clause which lists the join condition.
Students Departments
55. inner join = join
Select * from Department join student on id=dept_id;
56. left outer join = left join
Select * from Department left join student on id=dept_id;
57. right outer join = right join
Select * from Department right join student on id=dept _ id;
58. Pattern matching selection
Select * from Table where column like condition;
Select * from Departments where id like ‘%5’;
Select * from Departments where id like ‘_5’;
59. Ordered result selection
1) desc (descending order)
SELECT *
FROM departments
order by Batch desc;
2) asc (ascending order)
SELECT *
FROM departments
order by Batch asc;
60. The function to divide the tuples into groups and returns an aggregate
for each group.
Usually, it is an aggregate function’s companion
SELECT Batch, sum(Batch) as totalpatch
FROM Departments
group by Batch;
61. The substitute of WHERE for aggregate functions
Usually, it is an aggregate function’s companion
Example:
SELECT Batch, sum(Batch) as totalBatch
FROM Departments
group by Batch
having sum(Batch) > 10;
64. • PHP has the ability to access and manipulate any database that is
ODBC compliant
• PHP includes functionality that allows you to work directly with
different types of databases, without going through ODBC
65. Open a connection to a MySQL database server with the
mysql_connect() function
The mysql_connect() function returns a positive integer if it connects to
the database successfully or FALSE if it does not
Assign the return value from the mysql_connect() function to a variable
that you can use to access the database in your script
Close a connection to MySQL database server with the mysql_close()
function
66. The syntax for the mysql_connect()function is:
$connection = mysql_connect("host" , "user", "password");
if(!$connection)
{
die("Database connection filed: " .mysql_error());
}
The host argument specifies the host name or the where your MySQL
database server is installed
The user and pass arguments specify a MySQL account name and
password
67. This is also the syntax of connection
<?php
mysql_connect(“servername",“user",“password");
mysql_select_db (“database");
?>
68. This is also the syntax of connection
<?php
$server="localhost";
$user="root";
$pass=“password";
$database="school";
mysql_connect ($server , $user , $pass);
mysql_select _ db ($database);
?>
69. The connection will be closed automatically when the script ends.
To close the connection before, use the mysqli_close() function
<?php
$con=mysql_connect(“local host", “user", “password");
if (!$con)
{
echo "Failed to connect to MySQL: " . mysql_error();
}
mysqli_close($con);
?>
70. Reasons for not connecting to a database server include:
The database server is not running
Insufficient privileges to access the data source
Invalid username and/or password
The mysql_errno() and mysql_error() fuction used to show error
The mysql_errno() and mysql_error() functions return the results of
the previous mysql() function
71. The mysql_errno() function returns the error code from the last attempted
MySQL function call or 0 if no error occurred
<?php
$con = mysql_connect("localhost","root","Rashid0300");
if (!$con)
{ die('Could not connect: ' . mysql_errno()); }
mysql_close($con);
?>
72. The mysql_error() — Returns the text of the error message from previous
MySQL operation
<?php
$con = mysql_connect("localhost","root","Rashid0300");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
73. The syntax for the mysql_select_db() function is:
◦ mysql_select_db(database , connection);
The function returns a value of true if it successfully selects a database
or false if it does not
<?php
$db_select= mysql_select_db("WIDGET_CORP",$connection);
if(!$db_select)
{ die("Database not found: " .mysql_error()); }
?>