Pages

Friday 6 July 2012

PHP and XML

Let us first of all know what is XML.We easily say XML is Extened Markup Language which somehow feels like HTML which is also a markup language but much different from XML.Let us  now fhift to XML.
XML is a markup language that looks a lot like HTML. An XML document is plain text and contains tags delimited by < and >.There are two big differences between XML and HTML:
1.XML doesn't define a specific set of tags you must use.
2.XML is extremely picky about document structure.



In XML all the tags are "invented" by the author of the XML document.This is because the XML language has no predefined tag but tags used in HTML are predefined. HTML documents can only use tags defined in the HTML standard (like <p>, <h1>, etc.).XML allows the author to define his/her own tags and his/her own document structure.
XML is Not a Replacement for HTML but is a complement to HTML.XML is very strict when it comes to document structure. HTML let us play fast and loose with some opening and closing tags. But this is not the case with XML.



XML is used in many aspects of web development, often to simplify data storage and sharing.
1.XML Separates Data from HTML
2.XML Simplifies Data Sharing
3.XML Simplifies Data Transport
4.XML Simplifies Platform Changes
5.XML Makes our Data More Available
6.XML is Used to Create New Internet Languages

 

Now we have studied uses of XML let us now talk about its syntax rules:-
XML Syntax Rules

1.All XML Elements Must Have a Closing Tag.
For example:- <Message>This is incorrect</message>

2.XML Tags are Case Sensitive.

For example:-
<Msg>This is incorrect</msg>
 <msg>This is correct</msg>


3.XML Elements Must be Properly Nested.
For example:-
<b><i>This text is bold and italic</b></i>
//this is correct statement in html but incorrect in xml
4.XML Documents Must Have a Root Element.
XML documents contains one element that is the parent of all other elements. This element is called the root element.
The suntax is:-
<root>
<child>
<subchild>.....</subchild>
</child>


</root>

 
Now we take a simple example of xml:-


<bookstore> //it is simple root element
<book category="CHILDREN">
<title>Harry Potter</title> //subchild

<author>J K. Rowling</author>  //subchild
<year>2005</year>
          //subchild
  <price>29.99</price>    
//subchild
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Wednesday 4 July 2012

PHP+AJAX

Let us first of all know what is AJAX.AJAX stands for Asynchronous JavaScript and XML.AJAX is a new technique for creating better, faster, and more interactive web applications with the help of XML, HTML, CSS and Java Script.AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes.So,the point is we do not need to reload the whole page but just a small area undation will solve the purpose for client as well as server.
AJAX is based on internet standards, and uses a combination of:
1.XMLHttpRequest object (to exchange data asynchronously with a server)
2.JavaScript/DOM (to display/interact with the information)
3.CSS (to style the data)
4.XML (often used as the format for transferring data)
There is one main role of AJAX as the applications are browser- and platform-independent.
Let us take a simple example:-

<html>
 <head>
 <script language="javascript">

function postRequest(strURL)
{
var xmlHttp;
 if (window.XMLHttpRequest)  //For Mozilla, Safari, ...
{
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)  //For InternetXplorer
{
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('POST', strURL, true);
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4){
updatePageTime(xmlHttp.responseText);
}
}
xmlHttp.send(strURL);
}
function updatePageTime(str)
{
 document.getElementById("result").innerHTML =
"<font color='blue' size='6'>" + str + "</font>";;
}

function getCurrentServerTime(){
var rnd = Math.random();
var url="currentservertime.php?id="+rnd;
postRequest(url);
}

</script>

 </head>
 <body>
<div align="center">
<form><input type="button" value="Show current server time" onclick="getCurrentServerTime()"></div>
<div id="result" align="center"></div>
 </body>
</html>

In this program we will see a button in center of page displaying  (Show current server time) and when we click on this button we will see our system displays current date and time.Like below:-


Thursday Jul 05th, 2012, 15:56:59

Tuesday 3 July 2012

PHP + DATABASE

PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database.
For this we need to have mysql database.But don't worry we have a mysql database already in our XAMPP installed. We just need to open our XAMPP Control Panel and click on admin button placed along the side of stop button.This will open up your phpmyAdmin page on your WebBrowser.After this on window we will see option providing database creation.Set a Suitable name for your database and after this press the create button after that option of creating tables will appear .From now we need to give a name to our table,specify the number of columns in our table as well there datatypes and other attributes as required.This was all one need to know before beginning. 

Creating a Connection to a MySQL Database:-

The syntax for establishing a MySQL connection is a below. mysql_connect(servername,username,password);

Here mysql_connect is the inbuilt function and all other fields are the optional values for example servername  Specifies the server to connect to default value is "localhost", username Specifies the username to log in with. Default value is the name of the user that owns the server process and password Specifies the password to log in with. Default is "".

Let us now take a simple example:-

Example

In the following 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:

<?php
$con = mysql_connect("localhost","bajaj","bajaj123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


// some code
?> 

The output of above program is Could not connect only if no such username exists or the password is incorrect else a blank screen appears.

Now we study creating a database:-

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE Database test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create database: ' . mysql_error());
}
echo "Database test_db created successfully\n";
mysql_close($conn);
?>
If the output of above code is Database test_db created successfully
then it is sure that our database is created otherwise something is going
wrong and we need to check output error and solve it.
 
Selecting a Database:-
<?php
$dbhost = 'localhost';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass); //conn is the connection name
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'test_db' ); //test_db is the database name
mysql_close($conn);
?> 

Creating tables:-
 
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE employee( '.
       'emp_id INT NOT NULL AUTO_INCREMENT, './/proving column names and its attributes
       'emp_name VARCHAR(20) NOT NULL, '.     //proving column names and its attributes

       'emp_address  VARCHAR(20) NOT NULL, '. //proving column names and its attributes

       'emp_salary   INT NOT NULL, '.         //proving column names and its attributes

       'join_date    timestamp(14) NOT NULL, './/proving column names and its attributes

       'primary key ( emp_id ))';

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table employee created successfully\n";
mysql_close($conn);
?>