Friday, August 17, 2012

Inner Join and Outer Join


Inner join:
An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.


Examples
Suppose you have two Tables, with a single column each, and data as follows:
A    B
-    -
1    3
2    4
3    5
4    6
select * from a INNER JOIN b on a.a = b.b;
a | b
--+--
3 | 3
4 | 4
Left outer join:
A left outer join will give all rows in A, plus any common rows in B.
select * from a LEFT OUTER JOIN b on a.a = b.b;
a |  b  
--+-----
1 | null
2 | null
3 |    3
4 |    4
Full outer join:
A full outer join will give you the union of A and B, i.e. All the rows 
in A and all the rows in B.  If something in A doesn't have a 
corresponding datum in B, then the B portion is null, and vice versa.
select * from a FULL OUTER JOIN b on a.a = b.b;
 a   |  b  
-----+-----
   1 | null
   2 | null
   3 |    3
   4 |    4
null |    6
null |    5

Tuesday, August 14, 2012

Cookies & Sessions

Cookies

Cookies can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies, having their data stored on the client, work smoothly when you have a cluster of web servers, whereas sessions are stored on the server, meaning in one of your web servers handles the first request, the other web servers in your cluster will not have the stored information.

Using Cookies:
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]]);

Example for Cookies:
if (!isset($_COOKIE['Ordering'])) {
        
setcookie("Ordering", $_POST['ChangeOrdering'], time() + 31536000);
    }


Usage of Cookies:
echo $_COOKIE["Ordering"];

Destroy Cookies:
setcookie ("Ordering"""time() - 3600);

Sessions

Sessions are stored on the server, which means clients do not have access to the information you store about them - this is particularly important if you store shopping baskets or other information you do not want you visitors to be able to edit by hand by hacking their cookies. Session data, being stored on your server, does not need to be transmitted with each page; clients just need to send an ID and the data is loaded from the local file. Finally, sessions can be any size you want because they are held on your server, whereas many web browsers have a limit on how big cookies can be to stop rogue web sites chewing up gigabytes of data with meaningless cookie information.

To start a Session:
session_start();

Reading a session data:
$_SESSION['foo'] = 'bar';
print
 $_SESSION['foo'];

Removing a session data:
unset($_SESSION['foo']);

Ending a session:
session_destroy();

Checking a session data:
if (isset($_SESSION['FirstName'])) {
        
/// your code here
    
}