Search Your Project Here Please

Explain $_GET[] and $_POST[] super global array with proper examples.


Explain $_GET[]  and $_POST[] super global array with proper examples.

Ans:PHP $_GET:

PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
The example below shows the code in "test_get.php":

Example

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

PHP $_POST:

PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

Example

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
$name = $_POST['fname'];
echo $name;
?>

</body>
</html>

What is PHP ?How is it relates with HTML?
PHP:PHP is a server scripting language, and is a powerful tool for making dynamic and interactive Web pages quickly.
<?php
echo "My first PHP script!";
?>

There are two ways to use HTML on your PHP page. The first way is to put the HTML outside of your PHP tags. You can even put it in the middle if you close and reopen the <?php -and- ?> tags. Here is an example of putting the HTML outside of the tags:

<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>

<?php
 //your php code here
 ?>

<b>Here is some more HTML</b>

<?php
 //more php code
 ?>


</body>
</html>
The second way to use HTML with PHP is by using PRINT or ECHO. By using this method you can include the HTML inside of the PHP tags. Here is an example:
<?php
 Echo "<html>";
 Echo "<title>HTML with PHP</title>";
 Echo "<b>My Example</b>";

 //your php code here

 Print "<i>Print works too!</i>";
 ?>

7.How array is declared in PHP? also  explain various types of array with proper example .
Ans: Array declaration in PHP:
An array stores multiple values in one single variable.In PHP, the array() function is used to create an array.
Syntax:
array();
Example
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Array types:: There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

Indexed arrays - Arrays with numeric index.The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:
Example
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Associative arrays - Arrays with named keys.Associative arrays are arrays that use named keys that you assign to them.
Example
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Multidimensional arrays - Arrays containing one or more arrays.
Example
<?php
// A two-dimensional array:
$cars = array
  (
  array("Volvo",100,96),
  array("BMW",60,59),
  array("Toyota",110,100)
  );
?>
8.Differentiate between include() and require() . Also explain include once and require once(). Also list advantages of using both of them in php script.
Ans: Differentiate between include() and require():

Include()    Require()
1. include() emits a warning    1. require() emits an error
2. include will allow the script to continue.    2. require() results in a fatal error and does not allow the processing of the page
3. include will only produce a warning (E_WARNING) and the script will continue    3. require will produce a fatal error (E_COMPILE_ERROR) and stop the script

4. include 'filename';
    4. require 'filename';
5.
<?php
include 'header.php';
?>
    5.
<?php
require 'header.php';
?>


include_once:
The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement.
Example:
<?php
include_once "a.php"; // this will include a.php
include_once "A.php"; // this will include a.php again! (PHP 4 only)
?>

Require once: The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
Example:

<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>
Advantages of using include once():
1.    include_once with a case insensitive OS in PHP 4.
2.    It may help avoid problems such as function redefinitions, variable value reassignments, etc.
Advantages of using require once():
1.    This function can be recursive, thus building up a large PHP file with no require_once references at all.
2. require_once() is NOT independent of require()

9.Explain the advantage and disadvantage of mysql.
Ans:The advantages of MySQL are:                                                                                                      1.MySQL is an open source database system. Hence it can be downloaded and used by the developer for free.                                                                                                                            2.MySQL is robust and it provides excellent performance due to usage of MyISAM.                                  3.MySQL occupies very less disk space.                                                                                           4.MySQL can be easily installed in all major operating systems like Microsoft Windows, Linux, UNIX.                                                                                                                                   5.MySQL can be easily learnt using the tutorials that are available on internet.                                6.Though MySQL is open source, it offers most of the features provided by Oracle and other leading databases.                                                                                                                     7.MySQL is best suited for small and medium applications.                                                                        8.Since MySQL is acquired by Sun, Java is soon expected to include enhanced MySQL connectivity.
Disadvantages:
1.MySQL does not support a very large database size as efficiently
2.MySQL does not support ROLE, COMMIT, and Stored procedures in versions less than 5.0
3.Transactions are not handled very efficiently.

10.Explain following function with syntax ,purpose and example:
1.mysql_connect()
2.mysql_select_db()
3.mysql_query()
4.mysql_close()
Ans:mysql_connect(): The mysql_connect() function opens a new connection to the MySQL server.
Syntax:
mysql_connect(host,username,password,dbname,port,socket);
Purpose:
The purpose of this function is to open a new connection to the mysql server.
Example:
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_close($con);
?>
Mysql_close():
The mysql_close() function closes a non-persistent MySQL connection.
This function returns TRUE on success, or FALSE on failure.
Syntax:
mysql_close(connection)
Purpose:
The purpose of the mysql_close functionis to close connection mysql database with userid and password.The purpose of mysql_close() is also to save computer resources.
Example:
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_close($con);
?>
mysql_query():The mysql_query() function executes a query on a MySQL database.
This function returns the query handle for SELECT queries, TRUE/FALSE for other queries, or FALSE on failure.
Syntax:
mysql_query(query,connection)
Purpose:
To Submit a Query to MySQL from PHP.
Example:
<?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$sql = "SELECT * FROM Person";
mysql_query($sql,$con);

mysql_close($con);
?>
mysql_select_db():
The mysql_select_db() function sets the active MySQL database.
This function returns TRUE on success, or FALSE on failure.
Syntax:
mysql_select_db(database,connection)
Purpose:
The purpose of this function is to select a database.mysql_select_db() selects the current active database on the server.
Example:
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db", $con);

if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }

mysql_close($con);
?>
11.What is HTML attributes?Explain the HTML elements.
Ans:HTML attributes:
•    HTML elements can have attributes
•    Attributes provide additional information about an element
•    Attributes are always specified in the start tag
•    Attributes come in name/value pairs like: name="value"
Example:

<!DOCTYPE html>
<html>
<body>

<a href="http://www.w3schools.com">
This is a link</a>

</body>
</html>
HTML Elements:
•    An HTML element starts with a start tag / opening tag
•    An HTML element ends with an end tag / closing tag
•    The element content is everything between the start and the end tag
•    Some HTML elements have empty content
•    Empty elements are closed in the start tag
•    Most HTML elements can have attributes
Start tag *    Element content    End tag *
<p>    This is a paragraph    </p>
<a href="default.htm">    This is a link    </a>
<br>        

<!DOCTYPE html>
<html>

<body>
<p>This is my first paragraph.</p>
</body>

</html>
12.What are the necessities of using HTML form?Explain HTML form tag with its different attributes.
Ans: Necessities of using HTML form:

1.    HTML forms are used to pass data to a server.
2.    The <input> element is used to select user information.
3.    A submit button is used to send form data to a server.
4.    Radio buttons let a user select ONLY ONE of a limited number of choices.
5.    Checkboxes let a user select ZERO or MORE options of a limited number of choices.
HTML form tag with its different attributes:


id (name)
The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element in other instances (e.g., from client-side scripts).
<p id="paragraph1">This is the first paragraph, named "paragraph1". To dynamically change its properties use this identifier.</p>
class (cdata)
The "class" attribute assigns a class name (or a list of class names separated by spaces) to the container element. It is used together with style sheets and tells the browser the class (or classes) to which the element is associated.
A class gives presentational attributes to elements (read more at the Cascading Style Sheets tutorial).
<p class="references">This article is based on the book "Wind in the trees" by Jhon L. Brooks</p>
<p class="references important">This article is based on the book "Wind in the trees" by Jhon L. Brooks... and is more important than the one before.</p>
action (uri)
Points to a file that acts as processing agent for the form data. This agent will process the information as needed (compose an e-mail, save the data into a database, etc.).
If this attribute is not present, the form won't be sent.
<form action="../save-user-info.php">
...Form fields...
</form>
method
The "method" attribute establishes how the form's data will be sent to the processing agent. There are two possible values for this attribute (case-insensitive):
•    get: The form's data is added to the URI defined in the action attribute (e.g., "handler.php?pname=jhon&plastname=malcovich").
•    post: The form's data is added to the body of the form.
<form action="../save-user-info.php" method="post">
...Form fields...
</form>

13.Discuss the different types of links.
Ans: Links are used to “link” a visitor from one area to another.There area many types of links :

•    Local: A page on the same server or directory
•    Internal: A section on the current page or document
•    External: A page or site on a different server or directory
•    Download: A file for the visitor to download
•    E-mail: Opens the visitor’s e-mail program
1. Local Links:
<a href=”Page_in_the_same_server.html“>
Click here to go to the local page
</a>
2.Internal Links:
<A NAME=”ur bookmark name“></A>
Example:
<A NAME=”top“></A>
3. External Links:


<a href = “URL for the website“> text to click on </a>

Example:
<a href = “http”//me2learn.wordpress.com“> Click here to visit my blog, enjoy! </a>
4. Download links:

<a href=”File URL.zip“>

Clck here to download this file
</a>
5. E-mail links:
<a href= “mailto: mail_address@mail.com“>
click here ti send me e-mail </a>

Link colors:
•    LINK: Color of an non-activated link. (default blue)
•    VLINK: Color of a previously visited link. (default red)
•    ALINK: Color of a currently active link. (default orange)

<body link= “#0000ff“  vlink= “#ff0000”  alink= “#ff8429“  > Your HTML code here </body>
14.Design a HTML form for a company Book online that allows you to order the
books via internet . Form includes the following information:
(i) Book title, author, edition.
(i i ) Customer’s name, address, phone number.

Ans:

Book Title :     




Author :                        


Edition :                        

Customer's Name :                 



Address :                

Phone Number :               






RESET


SUBMIT




<html>
<head>
<title>FORM TO PURCHASE BOOK ONLINE</title>
</head>
<body>
<form action="#" method="get">
<table>
<tr>
  <td>Book Title:</td><td> <input type="text" name="fname"></td></tr>
<tr>
  <td>Author :</td><td> <input type="text" name="fname"></td></tr>
<tr>
  <td>Edition:</td><td> <input type="text" name="fname"></td></tr>
<tr>

  <td>Customer's Name:</td><td> <input type="text" name="fname"></td>
</tr>
<tr>
  <td>Address:</td><td> <input type="text" name="fname"></td></tr>
<tr>
  <td>Phone Number:</td><td> <input type="text" name="lname"></td></tr>
<tr>
  <td><input type="submit" value="SUBMIT"></td><td><input type="submit" value="RESET"></td>
</form>
</body>
</html>
15.Write steps to establish connection from PHP to mysql with sample code.
Ans:
There are 6 Steps to establish connection from PHP to mysql.These are:
Step 1 – Connecting to a MySQL database
Step 2 – Selecting the database to use
Step 3 – Performing a database query
Step 4 – Put the data on the page
Step 5 – Closing off the connection
Step 6 – Styling the Data

Sample code:
<html>
<head>
<title>Step 5</title>
</head>
<body>
 <?php
//Step1
$db = mysql_connect("localhost","username","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
//Step2
$db_select = mysql_select_db("databasename",$db);
if (!$db_select) {
die("database not found: " . mysql_error());
}
?>
<div class="cssstyle">
<?php
//Step3
$result = mysql_query("SELECT * FROM mytable", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
//Step4
while ($row = mysql_fetch_array($result)) {
echo "<h2>";
echo $row[1]."";
echo "</h2>";
echo "<p>";
echo $row[2]."";
echo "</p>";
}
?>
</div>

<?php
//Step5
mysql_close($db);
?>
</body>
</html>

16.Explain with example session and cookie variable in state management.
Ans:Session variable:A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.

Syntax:
session_start()

<?php
session_start();
…..,……,….
………………
session_destroy();
 ?>

Cookie: A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.
Syntax
setcookie(name, value, expire, path, domain);
Example:
In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:
 <?php
setcookie("user", "Alex Porter", time()+3600);
?>













 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...