PHP ORACLE TUTORIAL

How to connect oracle database with PHP? it's easy:

Requirement:
A. Webserver, Apache is the most popular webserver.
B. PHP5.
C. Oracle Client (Optional)

1. Or the most simple way is by download webserver package it will include all requirement except oracle client you can find a lot of this kind of package for the example phpTriad and so on but im using easyPHP coz it's much easiest to configuration than other webserver package.

2. And then install the easyPHP, after installation completed run easy php by executing .exe file, easyPHP icon will appear on right below of windows toollbars.

3. Right click the icon go to configuration and click PHP extensions and we need oci and oracle module as basic requirement for PHP to understand the Oracle functionality, check the PHP_oci8 and PHP_oracle or you can edit manually from php.ini by remove ";"

;extension=php_oci8.dll
;extension=php_openssl.dll
;extension=php_oracle.dll

after:

extension=php_oci8.dll
;extension=php_openssl.dll
extension=php_oracle.dll

and then restart the apache and php

4. Ok next steep is for PHP script:
Herewith the connection script to oracle database:

$db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.17.24.31)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = cbsms)))";
$conn = OCILogon("VIEWONLYSMS","smg",$db)
or die( "Could not connect to Oracle database!")
or die(ocierror());
?>
and below is very simple search engine PHP-Oracle and then then display the result:

1. Search Form:






2. Search PHP:

$db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = port number))(CONNECT_DATA = (SERVICE_NAME = sid)))";
$conn = OCILogon("username","password",$db)
or die( "Could not connect to Oracle database!")
or die(ocierror());

$query="select * from table where $_POST[select] like '%$_POST[sms_id]%'";
//put the oracle query

$statement = ociparse ($conn, $query);
ociexecute ($statement);
?>







while ($row = ocifetch ($statement,OCI_DEFAULT)){
?>



}
?>
5. And done it's easy right?

Edit Registration form

<?php
error_reporting(0);
include("config.php");

$editID=$_GET['edit'];

$select=mysql_query("SELECT * FROM registered_user WHERE userid=$editID");

while($row=mysql_fetch_array($select))
{
/*echo "<pre>";
print_r($row);
echo "</pre>";*/

$firstname=$row['firstname'];
$middlename=$row['middlename'];
$lastname=$row['lastname'];
$username=$row['username'];
$password=$row['password'];
$decodedPassword=base64_decode($password);
$seX=$row['sex'];
$preflocation=$row['preflocation'];
$pLocation=explode(",",$preflocation);
$address=$row['address'];
$stdcode=$row['stdcode'];
$phoneno=$row['phoneno'];
$countrycode=$row['countrycode'];
$mobileno=$row['mobileno'];
$cityid=$row['city_id'];
$stateid=$row['state_id'];
$country=$row['country'];
}

$firstName=trim($_POST['txtFirstName']);
$middleName=trim($_POST['txtMiddleName']);
$lastName=trim($_POST['txtLastName']);
$userName=trim($_POST['txtUserName']);
$password=trim($_POST['txtPassword']);
$txtReTypePassword=trim($_POST['txtReTypePassword']);
$sex=$_POST['sex'];
$txtPLocation=$_POST['txtPLocation'];
$txtAreaAddress=trim($_POST['txtAreaAddress']);
$txtSTDNo=trim($_POST['txtSTDNo']);
$txtPhoneNo=trim($_POST['txtPhoneNo']);
$txtCountryCode=trim($_POST['txtCountryCode']);
$txtMobileNo=trim($_POST['txtMobileNo']);
$selectState=$_POST['selectState'];
$selectCity=$_POST['selectCity'];
$txtCountry=trim($_POST['txtCountry']);

if(isset($_POST['btnEdit']) && ($_POST['btnEdit']=="Edit"))
{
echo "<pre>";
print_r($_POST);
echo "</pre>";

/* $str=implode(",",$txtPLocation);
echo "1".$str;
echo "</pre>";*/

if(empty($firstName))
{
$strError[] .="<li>Please Enter First Name</li>";
}
if(empty($middleName))
{
$strError[] .="<li>Please Enter Middle Name</li>";
}
if(empty($lastName))
{
$strError[] .="<li>Please Enter Last Name</li>";
}
if(empty($userName))
{
$strError[] .="<li>Please Enter Username</li>";
}
if(empty($password))
{
$strError[] .="<li>Please Enter Password</li>";
}
if(empty($txtReTypePassword))
{
$strError[] .="<li>Please Re-Type Password</li>";
}
if(empty($sex))
{
$strError[] .="<li>Please Select Sex</li>";
}
if(empty($txtPLocation))
{
$strError[] .="<li>Please Select Preffered Location</li>";
}
if(empty($txtAreaAddress))
{
$strError[] .="<li>Please Enter Address</li>";
}
if(empty($txtSTDNo))
{
$strError[] .="<li>Please Enter STD Code</li>";
}
if(empty($txtPhoneNo))
{
$strError[] .="<li>Please Enter Phone No</li>";
}
if(empty($txtCountryCode))
{
$strError[] .="<li>Please Enter Country Code</li>";
}
if(empty($txtMobileNo))
{
$strError[] .="<li>Please Enter Mobile No</li>";
}
if(empty($selectState))
{
$strError[] .="<li>Please Select State</li>";
}
if(empty($selectCity))
{
$strError[] .="<li>Please Select City</li>";
}
if(empty($txtCountry))
{
$strError[] .="<li>Please Enter Country</li>";
}
if($password != $txtReTypePassword)
{
$strError[] .="<li>Password is not matched</li>";
}
if(!empty($firstName) && !empty($middleName) && !empty($lastName) && !empty($username) && !empty($password) && !empty($txtReTypePassword) && !empty($sex) && !empty($txtPLocation) && !empty($txtAreaAddress) && !empty($txtSTDNo) && !empty($txtPhoneNo) && !empty($txtCountryCode) && !empty($txtMobileNo) && !empty($selectState) && !empty($selectCity) && !empty($txtCountry))
   {
/*$mysqlSelect = "SELECT * FROM tbl_admin WHERE username='".$username."'";
$mysqlSelected = mysql_query($mysqlSelect);

$count = mysql_num_rows($mysqlSelected);

if($count > 0)
{
$strError[] .= "<li> Username is already exist </li>";
}
else*/ if($password == $txtReTypePassword)
{
$encodedPassword=base64_encode($password);
$txtPrefLocation=implode(",",$txtPLocation);

$sqlUpdateRUser = "UPDATE registered_user SET
firstname='".$firstName."',
middlename='".$middleName."',
lastname='".$lastName."',
username='".$userName."',
password='".$encodedPassword."',
sex='".$sex."',
preflocation='".$txtPrefLocation."',
address='".$txtAreaAddress."',
stdcode='".$txtSTDNo."',
phoneno='".$txtPhoneNo."',
countrycode='".$txtCountryCode."',
mobileno='".$txtMobileNo."',
state_id='".$selectState."',
city_id='".$selectCity."',
country='".$txtCountry."'
WHERE userid='".$editID."'";

$sqlUpdatedRUser = mysql_query($sqlUpdateRUser);

$sqlUpdateAdmin = "UPDATE tbl_admin SET
username='".$userName."',
password='".$encodedPassword."'
WHERE admin_id='".$editID."'";

$sqlUpdatedAdmin = mysql_query($sqlUpdateAdmin);

header('Location: useredited.php');
}
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Registration Form</title>
<link type="text/css" href="css/style.css" rel="stylesheet" />
</head>
<body>
<div id="regiheading">Registration Form
</div>
<div id="regiwrap" align="center">
<table width="620px" border="0" cellpadding="0" cellspacing="0">
<tr>
<td id="regileftcorntop">
</td>
<td id="regiupperstrip">
</td>
<td id="regirightcorntop">
</td>
</tr>
<tr>
<td colspan="3">
<form name="registrationFrm" action="" method="post" id="registrationID" enctype="multipart/form-data" target="">
<div align="center">
<table border="0" width="620" cellspacing="0" cellpadding="5" align="center">
<tr>
<td id="sideborder">
</td>
<td>
</td>
<td>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td colspan="2" style="color:#FF0000; padding-left:100px;">
<?php foreach($strError as $key => $val)
 {  
echo $val;
 }
?>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">First Name :
</td>
<td id="namefields"><input type="text" name="txtFirstName" id="txtFirstNameId" value="<?php echo $firstname; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Middle Name :
</td>
<td id="namefields"><input type="text" name="txtMiddleName" id="txtMiddleNameId" value="<?php echo $middlename; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Last Name :
</td>
<td id="namefields"><input type="text" name="txtLastName" id="txtLastNameId" value="<?php echo $lastname; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Username :
</td>
<td id="namefields"><input type="text" name="txtUserName" id="txtUsernameId" value="<?php echo $username; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Password :
</td>
<td id="namefields"><input type="password" name="txtPassword" id="txtPasswordId" value="<?php echo $decodedPassword; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Re-Type Password :
</td>
<td id="namefields"><input type="password" name="txtReTypePassword" id="txtReTypePasswordId" value="<?php echo $decodedPassword; ?>" size="35"/>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Gender :
</td>
<td id="namefields"><input type="radio" name="sex" id="sexId" value="Male" <?php if($seX=='Male') echo 'checked'; ?>/>Male<span><input type="radio" name="sex" id="sexId" value="Female" <?php if($seX=='Female') echo 'checked'; ?>/>Female</span>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Prefferd Location :
</td>
<td id="namefields"><input type="checkbox" name="txtPLocation[]" id="txtPLocationId" value="Sydney" <?php if($pLocation[0]=="Sydney" || $pLocation[1]=="Sydney" || $pLocation[2]=="Sydney") echo 'checked'; ?>>Sydney<span><input type="checkbox" name="txtPLocation[]" id="txtPLocationId" value="Ottawa" <?php if($pLocation[0]=="Ottawa" || $pLocation[1]=="Ottawa" || $pLocation[2]=="Ottawa") echo 'checked'; ?>>Ottawa</span><span><input type="checkbox" name="txtPLocation[]" id="txtPLocationId" value="Singapore" <?php if($pLocation[0]=="Singapore" || $pLocation[1]=="Singapore" || $pLocation[2]=="Singapore") echo 'checked' ; ?>>Singapore</span>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Address :
</td>
<td id="namefields"><textarea cols="27" rows="5" name="txtAreaAddress" wrap=yes id="txtAreaAddressID"><?php echo $address; ?></textarea>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Phone Number :
</td>
<td id="namefields"><input type="text" name="txtSTDNo" id="txtSTDNoId" size="4" value="<?php echo $stdcode; ?>" />-<input type="text" name="txtPhoneNo" id="txtPhoneNoId" size="26" value="<?php echo $phoneno; ?>" />
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td>
</td>
<td style="font-size:12px;">STD Code<span style="margin-left:50px;">Phone Number</span>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Mobile Number :
</td>
<td id="namefields"><input type="text" name="txtCountryCode" value="<?php if($countrycode!='+91') echo $countrycode; else echo '+91';  ?>" size="4"  maxlength="3" />-<input type="text" name="txtMobileNo" id="txtMobileNoId" size="26" value="<?php echo $mobileno; ?>" />
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">State :
</td>
<td id="namefields"><select name="selectState" style="width:239px;">
<option value="0">--- Select State ---</option>
<?php
$state=mysql_query("select * from tbl_state");
while($data=mysql_fetch_array($state))
{
$state_id=$data['state_id'];
$state_name=$data['state_name'];
?>
<option value="<?Php echo $state_id; ?>" <?php if($stateid==$state_id)echo 'selected' ?>><?Php echo $state_name; ?>
</option>
<?php } ?>
</select>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">City :
</td>
<td id="namefields"><select name="selectCity" style="width:239px;">
<option value="0">---- Select City ----</option>
<?php
$city=mysql_query("select * from tbl_city");
while($data=mysql_fetch_array($city))
{
$city_id=$data['city_id'];
$city_name=$data['city_name'];
?>
<option value="<?Php echo $city_id; ?>" <?php if($cityid==$city_id) echo 'selected'?>><?Php echo $city_name; ?>
</option>
<?php } ?>
</select>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">Country :
</td>
<td id="namefields"><input type="text" name="txtCountry" id="txtCountryId" size="35" value="<?php echo $country; ?>" />
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td>
</td>
<td>
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td id="namelabel">
</td>
<td id="namefields"><input type="submit" name="btnEdit" value="Edit" />
</td>
<td id="sideborder">
</td>
</tr>
<tr>
<td id="sideborder">
</td>
<td>
</td>
<td>
</td>
<td id="sideborder">
</td>
</tr>
</table>
</div>
</form>
</tr>
<tr>
<td id="regileftcornbot">
</td>
<td id="regilowerstrip">
</td>
<td id="regirightcornbot">
</td>
</tr>
</table>
</div>
</body>
</html>

Writing your first PHP script

You already knew how to set up PHP environment in personal computer. I hope you set all require software to run PHP code. If you do not set up PHP environment in your pc, I suggest you at first set up PHP environment in your pc. In our whole tutorial we use vertrigo web server and dreamweaver editor. 

Step to write first PHP code

At first run your server by double clicking on vertrigo server icon. Now you have to go to the server folder where you installed it. Then click on VertrigoServ folder and click on www folder. If you use ZAMP, click on htdocs folder. This is the place where you will keep your PHP code. Open a file helloworld with .php extension. Now your PHP file name is helloworld.php. It is required to add .php extension in each PHP file otherwise it will not work.   
Open the PHP file using Notepad or Dreamweaver and write the following code.

<?php
echo ”Hello Everyone, This Is My First PHP Code”;
?>

Save the code. Now it is time to see your first PHP code. Open your browser and write http://localhost/helloworld. It shows Hello Everyone, This Is My First PHP Code. In this code <?php is the php opening tag and ?> is the end tag. So we write our code within this tag. echo is a PHP built in function. We use this function to print php code.  


PHP Operators

PHP Operators
Below listed in the tables are the php operators, it is important that you memorize at least few of them.

Arithmetic Operators

Search Result

Operator

Description

Example

Result

+

Addition

x=2
x+2

4

-

Subtraction

x=2
5-x

3

*

Multiplication

x=4
x*5

20

/

Division

15/5
5/2

3
2.5

%

Modulus (division remainder)

5%2
10%8
10%2

1
2
0

++

Increment

x=5
x++

x=6

--

Decrement

x=5
x--

x=4

Assignment Operators

Operator

Example

Is The Same As

=

x=y

x=y

+=

x+=y

x=x+y

-=

x-=y

x=x-y

*=

x*=y

x=x*y

/=

x/=y

x=x/y

%=

x%=y

x=x%y

Comparison Operators

Operator

Description

Example

==

is equal to

5==8 returns false

!=

is not equal

5!=8 returns true

>

is greater than

5>8 returns false

<

is less than

5<8>

>=

is greater than or equal to

5>=8 returns false

<=

is less than or equal to

5<=8 returns true

Logical Operators

Operator

Description

Example

&&

and

x=6
y=3

(x <> 1) returns true

||

or

x=6
y=3

(x==5 || y==5) returns false

!

not

x=6
y=3

!(x==y) returns true

PHP continue statement

PHP continue statement and break statement are identical but the only difference is PHP continue statement just skip the current iteration and continue execute other iteration inside loop. We can use continue statement in PHP iteration statement such as for loop, while loop and do-while loop. 

We can define PHP continue statement by using if statement such as if(condition) continue;  If the condition is true then the continue statement is executed.  Now we consider an example to understand the PHP continue statement.

PHP continue statement example:

<?php

for($count=0; $count<=10; $count++)

{

if($count==5)

continue;

echo $count;

}
In this example after if condition we use continue statement. So this program not print 5 because for continue statement. The output look like this where 5 is missing
1234678910

assignment operators in php



                            assignment operators in php     :      = , += , -= , *= , /= , .= , %=

<?php

$num1=10;
$num2=20;
echo ('<br>'. $num1." " .$num2);
?>


output :10 20




<?php
$num1=10;
$num2=20;
echo ('<br>'.$num2+=$num1);
?>


output :
30 





<?php


$num1=10;
$num2=20;
echo ('<br>'.$num2-=$num1
);?>


output :
10



<?php


$num1=10;
$num2=20;
echo ('<br>'.$num2*=$num1);
?>


output :
200





<?php


$num1=10;
$num2=20;


echo ('<br>'.$num2/=$num1);?>

output :
2



<?php
$num1=10;
$num2=20;
echo ('<br>'.$num2.=$num1);?>


output :
2010




<?php

$num1=10;
$num2=20;
echo ('<br>'.$num2%=$num1);

?>

output :
0