Saturday, August 21, 2010

Thursday, August 19, 2010

PHP Quiz

Guys!!!Let’s have some fun...here are some questions 4 u based on php. So ready????Here they are:

1. A script is a

a. Program or sequence of instructions that is interpreted or carried out by processor directly

b. Program or sequence of instruction that is interpreted or carried out by another program

c. Program or sequence of instruction that is interpreted or carried out by web server only

d. None of above



2. When compared to the compiled program, scripts run

a. Faster

b. Slower

c. The execution speed is similar

d. All of above



3. PHP is a widely used ……………. scripting language that is especially suited for web development and can be embedded into html

a. Open source general purpose

b. Proprietary general purpose

c. Open source special purpose

d. Proprietary special purpose

Wednesday, August 18, 2010

PHP and Database

Select Statement:


• So far, we have created a new table and inserted data into that table. In this lesson we will cover the most common MySQL Query that is used to retrieve information from a database.

• The SELECT statement is used to select data from a database.

Syntax: SELECT column_name(s)FROM table_name

Example: The following example selects all the data stored in the "Persons" table created in previous lesson.

<?php

$con = mysql_connect("localhost","root","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))

{

echo $row['FirstName'] . " " . $row['LastName'] ] . " " . $row['Age'];

echo "<br />";

}

mysql_close($con);

?>

Monday, August 16, 2010

PHP and Database

Create Statement:


• Create a Database:

• The CREATE DATABASE statement is used to create a database in MySQL.

Syntax: CREATE DATABASE database_name;

• To get PHP to execute the statement above we must use the mysql_query() function.

mysql_query - Send a MySQL query.

Description: resource mysql_query ( string query [, resource link_identifier] ).

• mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

Parameters :

query: A SQL query (The query string should not end with a semicolon).

link_identifier: The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

Return Values:

• For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE error.

• For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Saturday, August 14, 2010

PHP and Database

• Its tym 2 add some dynamic content 2 ur website. The best choice for ease-of-use, price and support is the combination of PHP and MySQL.
• MySQL is currently the most popular open source database server in existence.

• So what is a database actually??

• A database is a structure that comes in two flavors: a flat database and a relational database.

Flat database is a simple database design consisting of one large table instead of several interconnected tables. Called ‘flat’ because of its only two dimensional (data fields and records) structure, these database cannot represent complex data relationships. Also called flat file database or flatform database.

• Whereas, a relational database takes this "flat file" approach several logical steps further, allowing the user to specify information about multiple tables and the relationships between those tables, and often allowing much more declarative control over what rules the data in those tables must obey (constraints). These use language like SQL.

• In a relational structured database there are tables that store data. The columns define which kinds of information will be stored in the table. An individual column must be created for each type of data you wish to store (i.e. Age, Weight, Height).

• On the other hand, a row contains the actual values for these specified columns. Each row will have 1 value for each and every column. For example a table with columns (Name, Age, Weight-lbs) could have a row with the values (Bob, 65, 165).

Tuesday, August 10, 2010

Sessions in PHP

Sessions in PHP


• Hmm....so what are sessions actually???? 

• A session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user.

• A session is set up or established at a certain point in time, and torn down at a later point in time.

• A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses and HTTP communication is stateless.

• why use them???

• As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using sessions.

• A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next.

• PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

• It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

• Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

• Note: If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.

Monday, August 9, 2010

Cookies in PHP

 Cookies:


• Before discussing cookies in php, the question is what actually cookies are???? 

• Hmm... so a computer cookie is a small text file which contains a unique ID tag, placed on your computer by the website. The website saves a complimentary file with a matching ID tag. In this file various information can be stored, from pages visited on the site, to information voluntarily given to the site. When you revisit the site days or weeks later, the site can recognize you by matching the cookie on your computer with the counterpart in its database.

• U can also say that, a cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as an HTTP header by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.

• There are two types of computer cookies: temporary and permanent. Temporary cookies, also called session cookies, are stored temporarily in your browser's memory and are deleted as soon as you end the session by closing the browser. Permanent cookies, also called persistent cookies, are stored permanently on your computer's hard drive and, if deleted, will be recreated the next time you visit the sites that placed them there.

Thursday, August 5, 2010

Conditional Statements and PHP Functions

Conditional Statements:


• Like in C, in php too v have 4 types of conditional statements:

if statement - use this statement to execute some code only if a specified condition is true.

if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false.

if...elseif....else statement - use this statement to select one of several blocks of code to be executed.

switch statement - use this statement to select one of many blocks of code to be executed.

• These statements r used with same syntax as used in C.

Wednesday, August 4, 2010

Concatenation Operator and Form Handling

The Concatenation Operator:


• The concatenation operator (.) is used to put two strings together like (+) is used in java 4 concatenation.

• Consider the code:

<?php

$txt1=”Hello World!”;

$txt2=”What a nice day!”;

echo $txt1.” ”.$txt2;

?>

Display:

Hello World! What a nice day!

• If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.



PHP Form Handling:

• U all r familiar with form tag used in html. Let us see an html form having two input fields and one submit button.

<html>

<body>

<form action="welcome.php" method="post">

Name: <input type="text" name="fname" >

Age: <input type="text" name="age" >

<input type="submit" value=”SUBMIT”>

</form>

</body>

</html>

Monday, August 2, 2010

PHP Comment and PHP echo

PHP Comment:


• Syntax for single line comment-

   o <?php

   o #This is an example comment

   o //This is another example comment

   o echo "Sentence 1."; //This is our first statement

   o echo "<br />"; #This is a line break

   o echo "Sentence 2."; // This is our second statement

   o ?>

• U will notice that both types of commenting styles i.e. ‘//’ and ‘#’ r used on a single line .

• But it is very tedious task when needs to comment multiple lines. So there is another way of commenting. This style is used for multiple lines:

  o <?php

  o /* This is an easier

  o way to write comments

  o on multiple lines */

  o ?>

• So ‘/* and */’ is used for multiple line comments.


PHP-echo


• As u know php command echo is used 4 outputting text 2 a web browser but how to use echo command 4 displaying values stored in variables????

• Observe the following code:

<?php

$myString=”Hello!”;

echo $myString;

echo “<h5>I luv using PHP!!</h5>”;

?>

Display:

Hello!

I luv using PHP!!