PHP Examples - Files


PHP Examples - Files

Program:

<html >
<head>

<title>PHP Example 12 : FILES</title>
</head>

<body>

<?php
 
 echo "PHP Example 12";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"FILES";
 echo"</h1>";
 echo"<br/>";

 $filename="testfile.txt"; // Filename 
 
 $f1=fopen("$filename","w"); // Opening file in write mode
 echo "<br/>testfile.txt created.<br/>";
 fwrite($f1,"www.2k8618.blogspot.com"); // Writing data to file
 echo "<br/>Data written.<br/>";

 fclose($f1); //File closed
// echo "<br/>File closed.<br/>";


 $f2=fopen("$filename","r"); // Opening file in read mode

 $dataop= fread($f2,25); // Data read from file
 
 echo "<br/>Data read.<br/>";
 echo $dataop;
 fclose($f2);
  
 // unlink($filename); // to delete file
 

 echo"<br/>";

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

Output:



PHP Examples - Session - Pageviews

PHP Examples - Session - Number of Pageviews

Program:

<?php

 session_start(); // Starting PHP Session
 if(isset($_SESSION['pageviews'])) // Checking Session variable is set or not  
  $_SESSION['pageviews']+=1;
 else
  $_SESSION['pageviews']=1; // Setting Session variable 
 
 //echo $_SESSION['pageviews'];

?>  
<html >
<head>
<title>PHP EXAMPLE 11 : SESSION </title>
</head>

<body>

<?php
 
 echo "<br/>PHP Example 11";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"SESSION : Number of Pageviews";
 echo"</h1>";
 echo"<br/><br/>";
 echo "<b><h2> Number of Page Views: ".$_SESSION['pageviews']."</h2></b>";
 
 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>
</body>
</html>


Output:








PHP Examples - Student Details - Delete Data From Database - Form

PHP Examples - Student Details
Delete Data From Database -  Form

Program:

// php10.php

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP EXAMPLE 9 : Database with Form - Deletion </title>
</head>

<body>

<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Delete Data From DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 // echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
  die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  
  mysql_select_db("DB1",$con);  // Creating tables
  
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
 }
 $data= mysql_query(" SELECT Rollnum FROM Students ",$con); // Retreiving data
 echo"<center>
 <h2><b><u> STUDENT DETAILS </u></b></h2>
 <form method=\"post\" action=\"deletedata.php\"><br/><br/><br/>
 Select Roll Number: <select name=\"selection\">";

 while($rec=mysql_fetch_array($data))
 {
  echo "<option>".$rec['Rollnum']."</option>";  
 }
 
 echo "</select>
 <input type=\"submit\" value=\"Delete\" />
 </form>
 </center>";
 mysql_close($con);  // Closing connection
?>
</body>
</html>

// deletedata.php

<html >
<head>

<title>PHP EXAMPLE 9 : Database with Form - Deletion</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Delete Data From DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 //echo $_POST['selection'];
 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
 }
 $deleteqry="SELECT * FROM Students where Rollnum =". $_POST['selection'];
 $data1= mysql_query($deleteqry,$con); // Retreiving data
  
  //echo $data1;
  if($rec=mysql_fetch_array($data1))
   echo "<br/>Deleting...<br/>".$rec['Rollnum']." ".$rec['Name']." ".$rec['Mark']."<br/>";  
  else
   die( "<br/> Error in database access...".mysql_errno());  
  
  $deleterec = "DELETE FROM Students WHERE Rollnum=".$_POST['selection'];
 if(!mysql_query($deleterec,$con))
   die( "<br/> Error in deletion...".mysql_errno());  
 echo"<br><br>Record Deleted...<br/> Updated Table...";    

$data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

Output:



PHP Examples - Form - Insert Data To Database

PHP Examples - Form 
 Insert Data To Database

Program:
// php9.php
<html >
<head>
<title>PHP EXAMPLE 9 : Database with Form - Insertion </title>
</head>

<body>

<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Insert Form Data To DATABASE";
 echo"</h1>";
 echo"<br/>";
?>
<center>
<h2><b><u> STUDENT DETAILS </u></b></h2>
<form method="post" action="insertdata.php"><br/><br/><br/>
Roll No: <input type="text" value="" name="rollno"/><br/><br/><br/>
Name: <input type="text" value="" name="name1"/><br/><br/><br/>
Mark: <input type="text" value="" name="mark"/><br/><br/><br/>
<input type="submit" value="Submit" />

</form>
</center>
</body>
</html>




// insertdata.php

<html >
<head>

<title>PHP EXAMPLE 9 : Database with Form - Insertion</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 9";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"Insert Form Data To DATABASE";
 echo"</h1>";
 echo"<br/>";
 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed...'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
  mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
  echo"<br><br>Table  Students Created...";  
 }

 
  
 echo"<br><br>Table  Students Created...";  
   // Inserting data
 $insertst = "INSERT INTO Students VALUES ('$_POST[rollno]','$_POST[name1]','$_POST[mark]')";
 if(!mysql_query($insertst,$con))
   die( "<br/> Error...".mysql_errno());  
 echo"<br><br>Records added...<br/> Updated Table...";    
 $data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

PHP Examples - Cookie - Time of Last Visit


PHP Examples - Cookie - Time of Last Visit


Program:
<html >
<head>

<title>PHP Example 8 : COOKIE</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 8";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"COOKIE";
 echo"</h1>";
 echo"<br/>";
 echo "Server Time:\t".date("D,d M Y , g:i:s A")."<br/>";
 // echo "Cookie created...";

 if(isset($_COOKIE['time_of_visit']))
 {
  $y=$_COOKIE['time_of_visit'];
  echo "<br/>You visited here : $y";
 }
 $x=60*60*24*60+time();
 setcookie('time_of_visit',date("D,d M Y , g:i:s A"), $x);

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";

?>

</body>
</html>



Output:

PHP Examples - Function


PHP Example - Function

Program:
<html >
<head>

<title>PHP Example 6 : FUNCTIONS</title>
</head>

<body>
<?php
 
 echo "<br/>PHP Example 6";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"FUNCTIONS";
 echo"</h1>";
 echo"<br/>";
 
 function add($a,$b) //function for the addition
 {
  return $a+$b;
 }
 $x=20;
 $y=30;
 $z=add($x,$y);
 echo "<br/>Adding $x & $y   $z";
 
 echo "<br>Adding 10 & 20  ".add(10,20);
 
 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";

?>


</body>
</html>

Kannur University Exam Results - BTech 2012 - Semester 8


Kannur University Examination Results - Btech -2012
Semester 8 - Regular/Supplimentary/Improvement
  
Kannur university has announced the 8th semester btech results on 10th july 2012. The students can obtain their results from the website of kannur university.



PHP Examples - Databases - Mysql



PHP Examples 
 Databases - MySQL

  • Connect to database
mysql_connect("hostname","user","password");
  • Create database
mysql_query("CREATE DATABASE DB_name", connection);
  • Create table
mysql_query(" CREATE TABLE Table_name ( Name varchar(20),...)", connection);


...

Program:


<html >
<head>

<title>PHP Example 7 : DATABASES (MySQL)</title>
</head>

<body>
<?php
 
 echo "PHP Example 7";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"DATABASES: ( MySQL )";
 echo"</h1>";
 echo"<br/>";

 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
 }

 mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
  
 echo"<br><br>Table  Students Created...";  
   // Inserting data
 mysql_query(" INSERT INTO Students VALUES ('1','NIDHEESH','91')",$con);
 mysql_query(" INSERT INTO Students VALUES ('2','SAJITH','96')",$con);
 mysql_query(" INSERT INTO Students VALUES ('3','IRSHAD','88')",$con);
 mysql_query(" INSERT INTO Students VALUES ('4','AJISH','90')",$con);
 mysql_query(" INSERT INTO Students VALUES ('5','BINDU','90')",$con);
 mysql_query(" INSERT INTO Students VALUES ('6','RAFEEQ','95')",$con);
 echo"<br><br>Records added...";  
   
 $data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

PHP Examples - Arrays

PHP Examples 

 Arrays

Program:
<html >
<head>

<title>PHP Example 5 : ARRAYS</title>
</head>

<body>
<?php
 
 echo "PHP Example 5";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"ARRAYS";
 echo"</h1>";
 echo"<br/>";

 $array1 = array(5,3,7,6,1,2,9);  //Creating array 
 $array1[] = 8;
 $array1[] = 4;
 
 $n=count($array1); // Counting no of elements
 
 echo"<br/><b> Before sorting: </b> ";
 for($i=0;$i<$n;$i++)  //Printing array elements 
  echo $array1[$i]." ";  

 for($i=0;$i<$n;$i++)  //Sorting
 for($j=$i+1;$j<$n;$j++)
 {
  if($array1[$i]>$array1[$j])
  {
   $temp=$array1[$i];
   $array1[$i]=$array1[$j];
   $array1[$j]=$temp;
   }
 }
 
 echo"<br/> <b>After sorting: </b>";
 for($i=0;$i<$n;$i++)
  echo $array1[$i]." ";
 echo"<br/>";

 $arraysum = array_sum($array1);  // Calculating sum of the elements in the array 
 echo"<br/><b> Sum of the elements : </b>$arraysum";
 echo"<br/>";
 
 echo "<br/><b>Checking whether element is present in the array or not...</b>";
 if(in_array(5,$array1))      // Checking whether element is present in the array or not
  echo "<br/>5 is present in the array... ";
 else 
  echo "<br/>5 is not present in the array... ";
 echo"<br/>";

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>



Output:






PHP Examples - Strings - Substring - String Replacement - Reversal - Trim

PHP Examples - Strings
Substring - String Replacement - Reversal - Trim

Important Functions

1. Stringlength                       -            strlen(string)
2. Stringreversal                    -           strrev(string)
3. Substring                          -            substr(string, starting position, length)
4. Replace Substring              -            str_replace(new_substring, old_substring, string)
5. Trim (Removing Blank Spaces) -     rtrim(string) [ Remove blank space from right]

                                                   -     ltrim(string) [ Remove blank space from left]

Program:
<html >
<head>

<title>PHP Example 4 : STRING</title>
</head>

<body>
<?php
 
 echo "PHP Example 4";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"STRINGS";
 echo"</h1>";
 echo"<br/><li>";
 $string1 = "I LOVE 2K8CSE";
 echo"String: $string1";
 echo"<br/></li>";
 echo"<br/><li>";
 $stringlength= strlen($string1);   //Stringlength
 echo"length: $stringlength";
 echo"<br/></li>";
 echo"<br/><li>";
 $string2 = substr($string1,2,4); //Substring 
 echo" Substring (2,4): $string2";
 echo"<br/></li>";
 echo"<br/><li>";
 $string3= str_replace("I","WE",$string1); //Substring replacement
 echo"String after replacement (I by WE): $string3";
 echo"<br/></li>";
 echo"<br/><li>";
 $string4=strrev($string1);  //String reversal
 echo"String Reversal: $string4";
 echo"<br/></li>";
 echo"<br/><li>";
 $string5 =rtrim("I LOVE       ");    //Removing blank spaces from right of the string
 $string6=ltrim("       2K8CSE.");
 echo $string5.$string6."(removing blank spaces)";
 echo"<br/></li>"; 

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>
Output:







PHP Examples - Loops - While, Do While, For, Foreach

PHP Examples - Loops 
 While, Do While,  For, Foreach
Program:

<html >
<head>

<title>PHP Example 3 : Loops</title>
</head>

<body>
<?php
 
 echo "PHP Example 3";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"LOOPS";
 echo"</h1>";
 echo"<br/><li>";
 echo"WHILE LOOP: PRIME NUMBERS: ";
 echo"<br/>";
 $i=2;
 while($i<50)
 {
  $flag=true;
  $j=2;
  while($j<($i/2))
   if($i%$j==0)
   {
    $flag=false;
    break;
   }
   else
    $j++;
  if($flag)
  echo $i.' ';
  $i++;
 }
 echo"<br/></li>";

 echo"<br/><li>";

 echo"FOR LOOP: FIBONACCI NUMBERS";
 echo"<br/>";
 $t1=1;
 $t2=1;
 echo $t1.' '.$t2;
 for($i=1;$i<10;$i++)
 {
  $t3=$t1+$t2;
  $t1=$t2;
  $t2=$t3;
  echo ' '.$t3;
 }
 
 echo"<br/></li>";
  
  
 echo"<br/><li>";
 echo"DO WHILE LOOP: NATURAL NUMBERS ";
 echo"<br/>";
 $i=1;
 do{
  echo " $i";
  $i++;
 }while($i<=10);
 echo"<br/></li>";
 
 echo"<br/><li>";
 echo"FOR EACH LOOP: STUDENTS & MARKS "; 
 echo"<br/>";
 
   $mark["Mithun"]= "40";
   $mark["Shibin"]= "38";
   $mark["Manzoor"]= "35";
   $mark["Suhail"]= "45";
   $mark["Jishnu"]="36";
 
   foreach( $mark as $key => $value)
   {
    echo "Name: $key, Marks: $value <br/>"; 
   }
 echo"<br/></li>"; 

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

PHP Examples - Echo - PHP Program

Program:
<html >
<head>

<title>PHP Example 2</title>
</head>

<body>
<?php
 
 echo "PHP Example 2";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"ECHO";
 echo"</h1>";
 echo"<br/><li>";
 echo"Displays the text or values of a variables.";
 echo"</li><br/><li>"; // line break & list item
 
 $x="HELLO PHP..."; //Variable x
 echo $x;
 echo"</li><br/><li>"; 
 
 $y= 2;
 echo 'PHP Example:'.$y; 
 echo"</li><br/><li>"; 
 
 echo'PHP'; //single quotes
 echo"</li><br/><li>";
 
 echo"'PHP'"; //to display string in single quotes
 echo"</li><br/><li>";
 
 echo"\"PHP\""; //to display string in double quotes
 echo"</li>";

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>
Output:





A Simple PHP Program - PHP Examples

Program:

<html >
<head>

<title>PHP Example 1</title>
</head>

<body>
<?php
// comments

/* multiline
 comments
 examples
 */
 
 echo "PHP Example 1";
 echo"<br/>";
 echo"<h1>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</h1>";
 echo"<br/>";
 // Making text bold
 echo "<b>";
 echo  "www.2k8cse.tk";
 echo"</b>";
?>


</body>
</html>

Output:






Related Posts Plugin for WordPress, Blogger...