PHP Training - Lecture 4
PHP Training - Lecture 4
www.bits-sa.com
Web Development using PHP 2
Table of Contents
1. PHP Functions ........................................................................................................................................... 3
1.1 Create a PHP Function .................................................................................................................... 3
Example 1.1 ........................................................................................................................................... 3
1.2 PHP Functions - Adding parameters ............................................................................................... 3
Example 1.2 ........................................................................................................................................... 4
Example 1.3 ........................................................................................................................................... 4
1.3 PHP Functions - Return values ........................................................................................................ 4
Example 1.4 ........................................................................................................................................... 5
2. Using Databases ........................................................................................................................................ 5
2.1 Step 1 (Create a Connection to the database) ................................................................................ 5
Example 2.1 ........................................................................................................................................... 6
2.2 Step 2 (Write a query) ..................................................................................................................... 6
Example 2.2 ........................................................................................................................................... 6
2.3 Step 3 (Parse a query) ..................................................................................................................... 7
Example 2.3 ........................................................................................................................................... 7
2.4 Step 4 (Execute a query) ................................................................................................................. 8
Example 2.4 ........................................................................................................................................... 8
2.5 Step 5 (Fetch the result set) ............................................................................................................ 9
Example 2.5 ........................................................................................................................................... 9
2.6 Step 6 (Loop over the result set) ................................................................................................... 10
Example 2.6 ......................................................................................................................................... 10
7 Resources ................................................................................................................................................. 12
7.1 websites ........................................................................................................................................ 12
7.2 Books ............................................................................................................................................. 12
www.bits-sa.com
Web Development using PHP 3
1. PHP Functions
In this lecture we will show you how to create your own functions.
To keep the script from being executed when the page loads, you can put it into a function.
Syntax
function functionName()
{
code to be executed;
}
Give the function a name that reflects what the function does
The function name can start with a letter or underscore (not a number)
Example 1.1
A simple function that writes my name when it is called:
<html>
<body>
<?php
function display_name()
{
echo "My name is Zafar";
}
display_name ();
?>
</body>
</html>
Output:
My name is Zafar
www.bits-sa.com
Web Development using PHP 4
Parameters are specified after the function name, inside the parentheses.
Example 1.2
The following example will write different first names, but equal last name:
<html>
<body>
<?php
function display_name($fname)
{
echo "My name is ". $fname ;
}
display_name (“Zafar”);
?>
</body>
</html>
Output:
My name is Zafar
Example 1.3
The following function has two parameters:
<html>
<body>
<?php
function display_name($fname,$lname)
{
echo "My name is ". $fname . ' '. $lname;
}
display_name ("Zafar","Malik");
?>
</body>
</html>
Output:
www.bits-sa.com
Web Development using PHP 5
Example 1.4
<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
Output:
1 + 16 = 17
2. Using Databases
To use a database or query some table in a database, following are the typical steps to do;
www.bits-sa.com
Web Development using PHP 6
Example 2.1
Create a web page (blank page) with the name as dbcon.php in your htdocs/training/lecture4/
directory.
Type the following code in the file and run it in the browser.
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
}
?>
</body>
</html>
In this example we store the connection in a variable ($con) for later use in the script. The "die" part will
be executed if the connection fails.
So now if the connection is successful, we’ll now proceed to query the EMP table as follows;
Example 2.2
Write the bold line in your file as follows;
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
www.bits-sa.com
Web Development using PHP 7
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
}
?>
</body>
</html>
Note that This function does not validate query. The only way to find out if query is valid SQL statement
- is to execute it.
Example 2.3
Write the bold line in your file as follows;
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
www.bits-sa.com
Web Development using PHP 8
?>
</body>
</html>
Example 2.4
Write the bold line in your file as follows;
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
}
www.bits-sa.com
Web Development using PHP 9
$r = oci_execute($query_id);
?>
</body>
</html>
Example 2.5
Write the bold line in your file as follows;
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
}
www.bits-sa.com
Web Development using PHP 10
$r = oci_execute($query_id);
Example 2.6
Write the bold line in your file as follows;
<html>
<body>
<?php
// example 2.1 ..creating a database connection
$db_sid = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = zafarlaptop)(PORT = 1521)) )
(CONNECT_DATA = (SID = ORCL) ) )";
$db_user = "scott";
$db_pass = "tiger";
$con = oci_connect($db_user,$db_pass,$db_sid);
if($con)
{
echo "connection successful.";
}
else
{
die('Could not connect: ');
}
$r = oci_execute($query_id);
www.bits-sa.com
Web Development using PHP 11
7521
7566
7654
7698
7782
7788
7839
www.bits-sa.com
Web Development using PHP 12
7 Resources
7.1 websites
http://www.w3schools.com/
7.2 Books
Professional PHP Programming by Wrox word press Ltd. ISBN: 81-7366-201-0
www.bits-sa.com