Archive for May 3, 2008

Web Dev Interview Questions

Interview questions for Web developer position

  1. What’s the difference between primary key and unique key?
  2. The Perl script on the site is working slow. What’s your plan of attack?
  3. Name several reasons why a Web site would not work entirely.
  4. Write an SQL statement that would select only the customers who made multiple orders within a single week.
  5. What version control systems do you use?
  6. How do you configure the maximum number of processes in Apache?
  7. How much memory do you need for Web server app? Describe your logic of calculating this number.
  8. What are response codes 302 and 303?
  9. How do you delete all files with file extension temp recursively on a Linux box?
  10. What Perl tools do you use for coding, testing, debugging?
  11. Do you use DHTML and JavaScript? Describe the projects you’ve used them in.

^Back to Top

PHP interview questions, Part 1

  1. What are the differences between Get and post methods in form submitting, give the case where we can use get and we can use post methods?
  2. Who is the father of PHP and explain the changes in PHP versions?
  3. How can we submit a form without a submit button?
  4. In how many ways we can retrieve the date in the result set of mysql using PHP?
  5. What is the difference between mysql_fetch_object and mysql_fetch_array?
  6. What is the difference between $message and $$message?
  7. How can we extract string ‘abc.com ‘ from a string ‘http://info@abc.com’ using regular expression of PHP?
  8. How can we create a database using PHP and mysql?
  9. What are the differences between require and include, include_once?
  10. Can we use include (”abc.PHP”) two times in a PHP page “makeit.PHP”?
  11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10)) ?
  12. Functions in IMAP, POP3 AND LDAP?
  13. How can I execute a PHP script using command line?
  14. Suppose your Zend engine supports the mode <? ?> Then how can u configure your PHP Zend engine to support <?PHP ?> mode ?
  15. Shopping cart online validation i.e. how can we configure Paypal, etc.?
  16. What is meant by nl2br()?
  17. Draw the architecture of Zend engine?
  18. What are the current versions of apache, PHP, and mysql?
  19. What are the reasons for selecting lamp (linux, apache, mysql, PHP) instead of combination of other software programmes, servers and operating systems?
  20. How can we encrypt and decrypt a data present in a mysql table using mysql?
  21. How can we encrypt the username and password using PHP?
  22. What are the features and advantages of object-oriented programming?
  23. What are the differences between procedure-oriented languages and object-oriented languages?
  24. What is the use of friend function?
  25. What are the differences between public, private, protected, static, transient, final and volatile?
  26. What are the different types of errors in PHP?
  27. What is the functionality of the function strstr and stristr?
  28. What are the differences between PHP 3 and PHP 4 and PHP 5?
  29. How can we convert asp pages to PHP pages?
  30. What is the functionality of the function htmlentities?
  31. How can we get second of the current time using date function?
  32. How can we convert the time zones using PHP?
  33. What is meant by urlencode and urldocode?
  34. What is the difference between the functions unlink and unset?
  35. How can we register the variables into a session?
  36. How can we get the properties (size, type, width, height) of an image using PHP image functions?
  37. How can we get the browser properties using PHP?
  38. What is the maximum size of a file that can be uploaded using PHP and how can we change this?
  39. How can we increase the execution time of a PHP script?
  40. How can we take a backup of a mysql table and how can we restore it. ?
  41. How can we optimize or increase the speed of a mysql select query?
  42. How many ways can we get the value of current session id?
  43. How can we destroy the session, how can we unset the variable of a session?
  44. How can we destroy the cookie?
  45. How many ways we can pass the variable through the navigation between the pages?
  46. What is the difference between ereg_replace() and eregi_replace()?
  47. What are the different functions in sorting an array?
  48. How can we know the count/number of elements of an array?
  49. What is the PHP predefined variable that tells the What types of images that PHP supports?
  50. How can I know that a variable is a number or not using a JavaScript?
  51. List out some tools through which we can draw E-R diagrams for mysql.
  52. How can I retrieve values from one database server and store them in other database server using PHP?
  53. List out the predefined classes in PHP?
  54. How can I make a script that can be bilanguage (supports English, German)?
  55. What are the difference between abstract class and interface?
  56. How can we send mail using JavaScript?

Some definitions

Define PHP

The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

Mysql MySQL is a relational database management system, which means it stores data in separate tables rather than putting all the data in one big area. This adds flexibility, as well as speed. The SQL part of MySQL stands for “Structured Query Language,” which is the most common language used to access databases. The MySQL database server is the most popular open source database in the world.

URL

An acronym for “Uniform Resource Locator,” this is the address of a resource on the Internet. World Wide Web URLs begin with http://

AJAX

AJAX or Asynchronous JavaScript and XML is a term describing a web development technique for creating interactive web applications using a combination of: * HTML (or XHTML) and Cascading Style Sheets for presenting information* Document Object Model, JavaScript to dynamically display and interact with the information presented* XML, XSLT and the XMLHttpRequest object to interchange and manipulate data asynchronously with the web server (although AJAX applications can use other technologies … A scripting technique for silently loading new data from the server. Although AJAX scripts commonly use the soon to be standardized XMLHttpRequest object, they could also use a hidden iframe or frame. An AJAX script is useless by itself. It also requires a DOM Scripting component to embed the received data in the document.

Curl

cURL is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, Gopher, Telnet, DICT, FILE and LDAP. cURL supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, Kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, proxy tunneling and many other features.

send email using php

Simple mail sending script…

<?php

$to =  someone@example.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it
;

$subject = “Test mail”;

$message = “Hello! This is a simple email message.”;

$from =
someonelse@example.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it
;

$headers = “From: $from”;

mail($to,$subject,$message,$headers);

echo “Mail Sent.”;

?>

Mail with form

<html>

<body>

<?php

if (isset($_REQUEST['email']))

//if “email” is filled out, send email

{

//send email

$email = $_REQUEST['email'] ;

$subject = $_REQUEST['subject'] ;

$message = $_REQUEST['message'] ;

mail( “
someone@example.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it
“, “Subject: $subject”,

$message, “From: $email” );

echo “Thank you for using our mail form”;

}

else

//if “email” is not filled out, display the form

{

echo “<form method=’post’ action=’mailform.php’>

Email: <input name=’email’ type=’text’ /><br />

Subject: <input name=’subject’ type=’text’ /><br />

Message:<br />

<textarea name=’message’ rows=’15′ cols=’40′>

</textarea><br />

<input type=’submit’ />

</form>”;

}

?>

</body>

</html>

Some Mysql queries

BETWEEN

A useful way to test for the presence of a numeric or date value within a range is with the BETWEEN operator. This operator accepts two arguments, a minimum and maximum value, and tests whether the value provided falls within these two limits or not. If it does, the operator returns Boolean true; if not, it returns false. Here’s an example using numbers:
mysql> SELECT 2350 BETWEEN 100 AND 10000;
+—————————-+
| 2350 BETWEEN 100 AND 10000 |
+—————————-+
|                          1 |
+—————————-+
1 row in set (0.18 sec)

And here are two others involving dates:
mysql> SELECT 20060405 BETWEEN 20060101 AND 20070101;
+—————————————-+
| 20060405 BETWEEN 20060101 AND 20070101 |
+—————————————-+
|                                      1 |
+—————————————-+
1 row in set (0.00 sec)

mysql> SELECT 20060405 BETWEEN 20060101 AND 20060401;
+—————————————-+
| 20060405 BETWEEN 20060101 AND 20060401 |
+—————————————-+
|                                      0 |
+—————————————-+
1 row in set (0.00 sec)
GREATEST and LEAST

The GREATEST and LEAST operators provide a convenient shortcut to evaluating the largest or smallest in a group of numbers or temporal values. These operators are fairly self-explanatory — here’s an example of how the GREATEST operator works when used with a list of dates:
mysql> SELECT GREATEST(20000601, 20000529, 20000604);
+—————————————-+
| GREATEST(20000601, 20000529, 20000604) |
+—————————————-+
|                               20000604 |
+—————————————-+
1 row in set (0.00 sec)

And here’s an example of the LEAST operator applied to a list of numeric values:
mysql> SELECT LEAST(100, 200, 50, -6, -73, 1000);
+————————————+
| LEAST(100, 200, 50, -6, -73, 1000) |
+————————————+
|                                -73 |
+————————————+
1 row in set (0.03 sec)
IN

The IN operator is a useful way to test if a particular value exists in a pre-specified list of options. It can be used with numbers, strings and temporal values, and it accepts both the value to be tested and the list of options to test it against. Here’s an example using strings:
mysql> SELECT ‘c’ IN (‘a’, ‘b’, ‘c’, ‘d’);
+—————————–+
| ‘c’ IN (‘a’, ‘b’, ‘c’, ‘d’) |
+—————————–+
|                           1 |
+—————————–+
1 row in set (0.02 sec)

And here’s one using numbers:
mysql> SELECT 1 IN (3,4,5);
+————–+
| 1 IN (3,4,5) |
+————–+
|            0 |
+————–+
1 row in set (0.00 sec)
COALESCE

The COALESCE operator is particularly useful to distinguish between NULL and non-NULL values in a list. Supplied with a list of values, it either returns the first non-NULL value in the list or, if none such exist, it returns a NULL. This is particularly useful if, for example, you have a column containing both NULLs and actual content, and you wish to quickly “skip ahead” to the first non-NULL record. Here’s an example:
mysql> SELECT COALESCE(NULL, ‘bat’, 12, NULL);
+———————————+
| COALESCE(NULL, ‘bat’, 12, NULL) |
+———————————+
| bat                             |
+———————————+
1 row in set (0.02 sec)
INTERVAL

The INTERVAL operator offers yet another twist on numbers and their ranges. It accepts a list of numbers (N0, N1, N2…), tests N0 against each of the remaining numbers and returns the position index of the number that is immediately greater than N0. The numbers N1, N2… must be arranged in ascending order.

Consider the following example, which tests the number 24 against the list of numbers (5, 10, 15, 20, 25, 30):
mysql> SELECT INTERVAL (24, 5, 10, 15, 20, 25, 30);
+————————————–+
| INTERVAL (24, 5, 10, 15, 20, 25, 30) |
+————————————–+
|                                    4 |
+————————————–+

1 row in set (0.07 sec)

Here, 24 is less than 25 (index position 4) but more than 20 (index position 3), which is why the INTERVAL is 4.
STRCMP

The STRCMP() function is one of the simplest ways to compare strings in MySQL. The function accepts two arguments, the strings to be compared, and returns 0 if they’re the same, 1 if the first is larger than the second, and -1 if the first is smaller than the second. Comparison depends critically on the character set being used at the moment. Here are a couple of examples:
mysql> SELECT STRCMP(‘hell’, ‘hell’);
+————————+
| STRCMP(‘hell’, ‘hell’) |
+————————+
|                      0 |
+————————+
1 row in set (0.00 sec) mysql> SELECT STRCMP(‘bell’, ‘hell’);
+————————+
| STRCMP(‘bell’, ‘hell’) |
+————————+
|                     -1 |
+————————+
1 row in set (0.00 sec)
REGEXP

For more complex string comparison, the REGEXP function is often more powerful than the overused LIKE clause. This function tests an expression against a regular expression pattern, returning Boolean true if a match is found. Here’s an example:
mysql> SELECT ‘The Matrix’ REGEXP ‘[u-z]$’;
+——————————+
| ‘The Matrix’ REGEXP ‘[u-z]$’ |
+——————————+
|                            1 |
+——————————+
1 row in set (0.00 sec)
DATEDIFF

The DATEDIFF() function, as the name suggests, is a tool to quickly obtain the number of days between two dates. This is useful for calculating the number of days until an event occurs, for example, or to assess the age of a person given his or her date of birth.
mysql> SELECT DATEDIFF(20060101, 20051201);
+——————————+
| DATEDIFF(20060101, 20051201) |
+——————————+
|                           31 |
+——————————+
1 row in set (0.00 sec)

Some of DATEDIFF()’s most common uses arise in conjunction with the NOW() function — for example, to calculate the number of days between today and an arbitrary date in the past or future. Here’s an example:
mysql> SELECT DATEDIFF(NOW(), 20060203);
+—————————+
| DATEDIFF(NOW(), 20060203) |
+—————————+
|                       176 |
+—————————+
1 row in set (0.00 sec)

Mysql Join Queries Example Using MySQL, Joins

Workshop Requirements

You should have completed Parts One, Two, Three and Four of this series.

You should also have access to the MySQL command line client software.

You should also have full permissions on a database.
Introduction

In this Virtual Workshop we will look at retrieving data from a relational database structure, i.e. with multiple tables, using the SQL JOIN Syntax. Most databases have multiple tables to avoid repeating data. i.e. why enter the details of a customer over and over again.

NOTE: For brevity I will only show 5 records in the examples in this page.

MySQL – LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN
In a database such as MySQL, data is divided into a series of tables (the “why” is beyond what I’m writing today) which are then connected together in SELECT commands to generate the output required. I find when I’m running MySQL training, people often get confused between all the join flavours. Let me give you an example to see how it works.

If this isn’t quite the question you’re looking to have answered, we’ve got a MySQL IAQ (Infrequently Answered Questions that may help you

First, some sample data:
Mr Brown, Person number 1, has a phone number 01225 708225
Miss Smith, Person number 2, has a phone number 01225 899360
Mr Pullen, Person number 3, has a phone number 01380 724040
and also:
Person number 1 is selling property number 1 – Old House Farm
Person number 3 is selling property number 2 – The Willows
Person number 3 is (also) selling property number 3 – Tall Trees
Person number 3 is (also) selling property number 4 – The Melksham Florist
Person number 4 is selling property number 5 – Dun Roamin.

mysql> select * from demo_people;

+————+————–+——+

| name       | phone        | pid  |

+————+————–+——+

| Mr Brown   | 01225 708225 |    1 |

| Miss Smith | 01225 899360 |    2 |

| Mr Pullen  | 01380 724040 |    3 |

+————+————–+——+

3 rows in set (0.00 sec)

mysql> select * from demo_property;

+——+——+———————-+

| pid  | spid | selling              |

+——+——+———————-+

|    1 |    1 | Old House Farm       |

|    3 |    2 | The Willows          |

|    3 |    3 | Tall Trees           |

|    3 |    4 | The Melksham Florist |

|    4 |    5 | Dun Roamin           |

+——+——+———————-+

5 rows in set (0.00 sec)

mysql>

If I do a regular JOIN (with none of the keywords INNER, OUTER, LEFT or RIGHT), then I get all records that match in the appropriate way in the two tables, and records in both incoming tables that do not match are not reported:

mysql> select name, phone, selling

from demo_people join demo_property

on demo_people.pid = demo_property.pid;

+———–+————–+———————-+

| name      | phone        | selling              |

+———–+————–+———————-+

| Mr Brown  | 01225 708225 | Old House Farm       |

| Mr Pullen | 01380 724040 | The Willows          |

| Mr Pullen | 01380 724040 | Tall Trees           |

| Mr Pullen | 01380 724040 | The Melksham Florist |

+———–+————–+———————-+

4 rows in set (0.01 sec)

mysql>

If I do a LEFT JOIN, I get all records that match in the same way and IN ADDITION I get an extra record for each unmatched record in the left table of the join – thus ensuring (in my example) that every PERSON gets a mention:

mysql> select name, phone, selling

from demo_people left join demo_property

on demo_people.pid = demo_property.pid;

+————+————–+———————-+

| name       | phone        | selling              |

+————+————–+———————-+

| Mr Brown   | 01225 708225 | Old House Farm       |

| Miss Smith | 01225 899360 | NULL                 |

| Mr Pullen  | 01380 724040 | The Willows          |

| Mr Pullen  | 01380 724040 | Tall Trees           |

| Mr Pullen  | 01380 724040 | The Melksham Florist |

+————+————–+———————-+

5 rows in set (0.00 sec)

mysql>

If I do a RIGHT JOIN, I get all the records that match and IN ADDITION I get an extra record for each unmatched record in the right table of the join – im my example, that means that each property gets a mention even if we don’t have seller details:

mysql> select name, phone, selling

from demo_people right join demo_property

on demo_people.pid = demo_property.pid;

+———–+————–+———————-+

| name      | phone        | selling              |

+———–+————–+———————-+

| Mr Brown  | 01225 708225 | Old House Farm       |

| Mr Pullen | 01380 724040 | The Willows          |

| Mr Pullen | 01380 724040 | Tall Trees           |

| Mr Pullen | 01380 724040 | The Melksham Florist |

| NULL      | NULL         | Dun Roamin           |

+———–+————–+———————-+

5 rows in set (0.00 sec)

Joins concepts-Mysql with Using MySQL, Joins

Workshop Requirements

You should have completed Parts One, Two, Three and Four of this series.

You should also have access to the MySQL command line client software.

You should also have full permissions on a database.
Introduction

In this Virtual Workshop we will look at retrieving data from a relational database structure, i.e. with multiple tables, using the SQL JOIN Syntax. Most databases have multiple tables to avoid repeating data. i.e. why enter the details of a customer over and over again.

NOTE: For brevity I will only show 5 records in the examples in this page.
Creating Another Table

In order to see how JOINs work we need to create another table and populate it with data. So let’s create a new table called ‘genres’ with the following properties.
genreID     Unique Identifier     01
genre     Music Genre     Heavy Metal
boughtby     Type of person that buys this music     Greasy Haired Bikers

mysql> CREATE TABLE genres (
-> genreID INT(2) auto_increment primary key,
-> genre VARCHAR(20),
-> boughtby VARCHAR(30)
->);

Insert the following genres into your genres table. The descriptions for the people that buy the music (boughtby) – I’ll leave to you. You could also add other genres that are perhaps more relevant to your music collection.
Pop     Example INSERT:

mysql> INSERT INTO genres VALUES (
-> ‘?’,
-> ‘Heavy Metal’,
-> ‘Greasy Haired Bikers’
-> );

Easy Listening
‘Classic’ Rock
Heavy Metal
Soul
Seventies
Eighties
Hip Hop
Jazz
Guitar Bands

This could (depending on what you enter as descriptions) result in a table like this:

mysql> SELECT *
-> FROM genres;
+———+—————-+————————————————+
| genreID | genre          | boughtby                                       |
+———+—————-+————————————————+
|       1 | Pop            | Little girls and adults who should know better |
|       2 | Easy Listening | Crushed velvet wearing lounge lizards          |
|       3 | “Classic” Rock | Middle-aged men reliving their youth           |
|       4 | Heavy Metal    | Wannabe Bikers – who work PT at safeway        |
|       5 | Soul           | White Boys in thin leather ties                |
|       6 | Seventies      | Those not born til 1980                        |
|       7 | Eighties       | Those born in the ’70’s                        |
|       8 | Hip Hop        | Middle-class Ghetto wannabes                   |
|       9 | Jazz           | Those that *think* they are better             |
|      10 | Guitar Bands   | Those stuck in 1996                            |
+———+—————-+————————————————+
10 rows in set (0.00 sec)

Adding a genre to the ‘cds’ table

Next we need to add a ‘genreID’ column to our ‘cds’ table so we can store information about the genre of each cd. This will be an Integer (numeric) field as it will correspond to the unique identifier (‘genreID’) from the genres table.

mysql> ALTER TABLE cds
-> ADD genreID INT(2);

Check this has worked by using the describe command.

mysql> DESCRIBE cds;
+——–+————-+——+—–+———+—————-+
| Field  | Type        | Null | Key | Default | Extra          |
+——–+————-+——+—–+———+—————-+
| cdID   | int(3)      |      | PRI | NULL    | auto_increment |
| artist | varchar(20) | YES  |     | NULL    |                |
| title  | varchar(30) | YES  |     | NULL    |                |
| year   | int(4)      | YES  |     | NULL    |                |
| label  | varchar(20) | YES  |     | NULL    |                |
| bought | date        | YES  |     | NULL    |                |
| tracks | int(2)      | YES  |     | NULL    |                |
| genreID| int(2)      | YES  |     | NULL    |                |
+——–+————-+——+—–+———+—————-+
8 rows in set (0.00 sec)

IMPORTANT: As we now have a column called ‘genreID’ in both tables we need to distinguish which we are talking about. This is why we have been prefixing the column name with the table name to ensure we can tell the difference. For example ‘cds.genreID’ and ‘genres.genreID’ are easily distinguishable in our example.

We are now ready to enter the genre type for each cd into our CDs table.

If we decide that our ‘Jamiroquai’ album is a soul album we need the ‘genres.genreID’ number from our ‘genres’ table for the soul entry, i.e. ‘5′. We must UPDATE the Jamiroquai record in the CDs table so that the ‘cds.genreID’ column also has a value of ‘5′.

mysql> UPDATE cds
-> SET cds.genreID = 5
-> WHERE (cds.cdID = 2);

If we issue a SELECT all command we can see the effect this has.

mysql> select * from cds;
+——+——————-+——————+——+———+
| cdid | artist            | title            | year | genreID |
+——+——————-+——————+——+———+
|    2 | Jamiroquai        | A Funk Odyssey   | 2001 |     5   |

Continue and enter the rest of the genres into the CDs table.
Beginning with Joins

Before starting with joins we should say a little about what exactly a join is. Basically it is the combining of two rows based on the comparative values in selected columns. This ’super-row’ exists only for the duration of the query that creates it. We need to use joins to temporarily create ‘complete’ records from a database which may split related data across several tables (perhaps as a result of normalisation).
Cross-Join

Syntax:

SELECT <column_name>
FROM <table1>, <table2>

A cross-join between two tables takes the data from each row in table1 and joins it to the data from each row in table2. To give an example lets look at two very simple tables.
id     animal
1     Cat
2     Dog
3     Cow
id     Food
1     Milk
2     Bone
3     Grass

A cross-join on these tables would produce the following result.
1     Cat     1     Milk
1     Cat     2     Bone
1     Cat     3     Grass
2     Dog     1     Milk
2     Dog     2     Bone
2     Dog     3     Grass
3     Cow     1     Milk
3     Cow     2     Bone
3     Cow     3     Grass

Where every row from one table is joined to every row in the other table. We can also see the effect of this by using an SQL cross-join on our tables (although in this example we are asking only to display one column from each table).

mysql> SELECT cds.artist, genres.genre
-> FROM cds, genres;

You should see every artist associated with every genre. This is obviously not a very useful join, but does specify the need for a join that uses some kind of comparison between the two tables.
The Equi-Join or Inner Join

Syntax:

SELECT <column_name>
FROM <Table1>, <Table2>
WHERE (Table1.column = Table2.column)

In the equi-join the comparison we are making between two columns is that they match the same value. We can use this method to select certain fields from both tables and only the correct rows will be joined together. So if we were to use this join on the cds and genres tables in our own database (using the CDs that Mark provided in the last virtual workshop as an example).

mysql> SELECT cds.artist, cds.title, genres.genre
-> FROM cds, genres
-> WHERE (cds.genreID = genres.genreID);
+————+——————————+—————-+
| artist     | title                        | genre          |
+————+——————————+—————-+
| Jamiroquai | A Funk Odyssey               | Soul           |
| Abba       | Abbas Greatest Hits          | Seventies      |
| Various    | Now 49                       | Pop            |
| westlife   | westlife                     | Pop            |
| Various    | Eurovision Song contest 2001 | Easy Listening |
+————+——————————+—————-+
5 rows in set (0.00 sec)

Obviously your data will be different as you should have different CDs and different genres. If we compare the cross-join and equi-join we can see that the equi-join is just the cross join with a very restrictive WHERE condition, that forces only the rows in the second table RELEVANT to the rows in the first table to be retrieved. This method is fine if all we want to do is look at normalised data in a temporarily flat database view. However if we want to filter this data, we have to start adding more conditions to our WHERE clause and it seems rather redundant to have to specify joining conditions as part of the WHERE condition every time.
The Left Join

The left join is a mechanism used to join tables before we add other conditions such as WHERE etc.

Syntax:

SELECT <column_name>
FROM <Table1>
LEFT JOIN <Table2>
ON Table1.column = Table2.column

Without adding a WHERE clause the Left Join produces the same results as the equi-join example above.

mysql> SELECT cds.artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID;
+——————-+——————————+—————-+
| artist            | title                        | genre          |
+——————-+——————————+—————-+
| Jamiroquai        | A Funk Odyssey               | Soul           |
| Abba              | Abbas Greatest Hits          | Seventies      |
| Various           | Now 49                       | Pop            |
| westlife          | westlife                     | Pop            |
| Various           | Eurovision Song contest 2001 | Easy Listening |
+——————-+——————————+—————-+
5 rows in set (0.00 sec)

An important thing to note with this particular join is that even if there are no records in the second table (in this case ‘genres’) data will still be displayed from the first table. Or in other words data from the LEFT of the join will be displayed and is where the term LEFT JOIN comes from. To demonstrate this, UPDATE a genreID from row four (cdID of 5, because we deleted one row in part three) of the cds table (in this case westlife) to a value that doesn’t exist in the genres table.

mysql> UPDATE cds
SET cds.genreID = 100
WHERE (cds.cdID = 5);

…and run the query again….

mysql> SELECT cds.artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID;
+——————-+——————————+—————-+
| artist            | title                        | genre          |
+——————-+——————————+—————-+
| Jamiroquai        | A Funk Odyssey               | Soul           |
| Abba              | Abbas Greatest Hits          | Seventies      |
| Various           | Now 49                       | Pop            |
| westlife          | westlife                     | NULL           |
| Various           | Eurovision Song contest 2001 | Easy Listening |
+——————-+——————————+—————-+
5 rows in set (0.00 sec)

The artist and title are still displayed even though there is no data in the genre (and thus NULL is shown). To further illustrate this we can issue a RIGHT JOIN which is a variation where all the data on the RIGHT side of the join (the second table) is returned regardless of the presence of data from the first table.

Reset row four of the cds table to have the correct genreID value.

mysql> UPDATE cds
-> SET cds.genreID = 1
-> WHERE (cds.cdID = 5);

And run the RIGHT JOIN query including genres.genreID.

mysql> SELECT cds.artist, cds.title, genres.genreID, genres.genre
-> FROM cds
-> RIGHT JOIN genres
-> ON cds.genreID = genres.genreID;
+——————–+——————————+———+—————-+
| artist             | title                        | genreID | genre          |
+——————–+——————————+———+—————-+
| Various            | Now 49                       |       1 | Pop            |
| westlife           | westlife                     |       1 | Pop            |
| Various            | Eurovision Song contest 2001 |       2 | Easy Listening |
| NULL               | NULL                         |       3 | “Classic” Rock |
| NULL               | NULL                         |       4 | Heavy Metal    |
| Jamiroquai         | A Funk Odyssey               |       6 | Soul           |
| Abba               | Abbas Greatest Hits          |       6 | Seventies      |
| NULL               | NULL                         |       7 | Eighties       |
| NULL               | NULL                         |       8 | Hip Hop        |
| NULL               | NULL                         |       9 | Jazz           |
| NULL               | NULL                         |      10 | Guitar Bands   |
+——————–+——————————+———+—————-+
19 rows in set (0.00 sec)

Note that where there aren’t any cds in a genre then a NULL value is returned. This is because every record of the RIGHT side must be returned at least once by the RIGHT JOIN.
Adding a WHERE Clause to our Join

Now we have the join occurring out with the WHERE clause, we can begin to add other conditions. For example if we want to select only the pop CDs

mysql> SELECT cds.artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> WHERE genres.genre = ‘pop’;
+———-+———-+——-+
| artist   | title    | genre |
+———-+———-+——-+
| Various  | Now 49   | Pop   |
| westlife | westlife | Pop   |
+———-+———-+——-+
2 rows in set (0.00 sec)

The USING Clause

A variation on the Left Join is the ‘USING’ clause. You can use this if the columns you are carrying out the join on have the same name.

Syntax:

SELECT <column_name>
FROM <Table1>
LEFT JOIN <Table2>
USING (<column_name>)

In our example we are joining the tables where cds.genreID is the same as genres.genreID thus genreID is the name of a column in BOTH of tables we are using for the join.

mysql> SELECT cds.artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> USING (genreID);
+——————-+——————————+—————-+
| artist            | title                        | genre          |
+——————-+——————————+—————-+
| Jamiroquai        | A Funk Odyssey               | Soul           |
| Abba              | Abbas Greatest Hits          | Seventies      |
| Various           | Now 49                       | Pop            |
| westlife          | westlife                     | Pop            |
| Various           | Eurovision Song contest 2001 | Easy Listening |
+——————-+——————————+—————-+
5 rows in set (0.00 sec)

Mini Exercise

Practice joining the ‘CDs’ and ‘genre’ tables to retrieve different data about the CDs, e.g.

mysql> SELECT cds.artist, genres.boughtby
-> FROM cds
-> LEFT JOIN genres
-> USING (genreID);
+——————-+————————————————+
| artist            | boughtby                                       |
+——————-+————————————————+
| Jamiroquai        | White Boys in thin leather ties                |
| Abba              | Those not born til 1980                        |
| Various           | Little girls and adults who should know better |
| westlife          | Little girls and adults who should know better |
| Various           | Crushed velvet wearing lounge lizards          |
+——————-+————————————————+
5 rows in set (0.00 sec)

Preparing to Join more than two tables

It is also possible to join more than two tables. In order to do this, however, we will need to make a third table – this time an ‘artist’ table containing the artistID and name. This will use a lot of the techniques we’ve used in the previous workshops, so you may have to refer back to refresh your memory as to what you are doing, which will serve as more good revision. We are going to create our new table using a five stage process

* CREATE the new table
* INSERT our artist data using a SELECT DISTINCT query
* ALTER TABLE to add a new ‘artistID’ COLUMN to the cds TABLE
* UPDATE the cds.artistID fields to match the values assigned to an artist in the new table
* DROP the artist column from the cds TABLE

We’ll go through each step in more detail
CREATE the new table

A simple enough revision of what we’ve done before.

mysql> CREATE TABLE artists (
-> artistID int(3) auto_increment primary key,
-> Artist varchar(20)
-> );

INSERT our artist data using a SELECT DISTINCT query

This uses similar syntax to that in Workshop Four. We are going to insert the artists from the cds table into the artist.Artist column.

mysql> INSERT INTO artists (artists.Artist)
->  SELECT DISTINCT cds.artist
->  FROM cds;

You can check this has worked by using a (by now) standard SELECT statement.

mysql> SELECT *
-> FROM artists;

ALTER TABLE to add a new ‘artistID’ COLUMN to the cds TABLE

As we are going to refer to the artist by their artistID rather than their name, we need to create a column in the cds table to hold that ID.

mysql> ALTER TABLE cds
-> ADD artistID int(3);

Check this has worked with a describe statement.

mysql> DESCRIBE cds;
+———-+————-+——+—–+———+—————-+
| Field    | Type        | Null | Key | Default | Extra          |
+———-+————-+——+—–+———+—————-+
| cdID     | int(3)      |      | PRI | NULL    | auto_increment |
| artist   | varchar(20) | YES  |     | NULL    |                |
| title    | varchar(30) | YES  |     | NULL    |                |
| year     | int(4)      | YES  |     | NULL    |                |
| label    | varchar(20) | YES  |     | NULL    |                |
| bought   | date        | YES  |     | NULL    |                |
| tracks   | int(2)      | YES  |     | NULL    |                |
| genreID  | int(2)      | YES  |     | NULL    |                |
| artistID | int(3)      | YES  |     | NULL    |                |
+———-+————-+——+—–+———+—————-+
9 rows in set (0.00 sec)

UPDATE the cds.artistID fields without Joins

For those with access to older versions of MySQL this will have to be done by hand. If you are lucky enough to have MySQL 4.0 installed, then you could use UPDATE joins to speed up this stage, but for consistency between versions MySQL I will demonstrate the method common to older and newer versions of MySQL.

mysql> UPDATE cds
-> SET cds.artistID = 1
-> WHERE (cds.artist = ‘Jamiroquai’);

……etc usng all the artistIDs from the artists table. Check that this has worked with a few joins between the cds and artists TABLEs eg

mysql> SELECT artists.Artist, cds.title
-> FROM artists
-> LEFT JOIN cds
-> USING (artistID)
-> WHERE (cds.artistID = 1);
+————+—————-+
| name       | title          |
+————+—————-+
| jamiroquai | A Funk Odyssey |
+————+—————-+

DROP the artist column from the cds TABLE

As we can retrieve the artist name by using a join and the artistID we can remove the cds.artist column.

mysql> ALTER TABLE cds
-> DROP artist;
Query OK, 16 rows affected (0.15 sec)
Records: 16  Duplicates: 0  Warnings: 0

We are now ready to join all three tables together.
Joining Three Tables

Before we start with statements let’s just recap what we can expect to do. We have a cds table that contains the foreign keys (i.e. values that correspond to primary keys in another table) called cds.genreID and cds.artistID which also exist in the genres and artists tables. A three table join can be achieved using another version of the Equi-Join or Inner Join where we can use the WHERE clause to limit the returned records based on comparing the artistID and the genreID.

mysql> SELECT artists.Artist, cds.title, genres.genre
-> FROM cds, artists, genres
-> WHERE (
-> (cds.artistID = artists.artistID)
-> AND (cds.genreID = genres.genreID)
-> );
+—————–+——————————+—————-+
| name            | title                        | genre          |
+—————–+——————————+—————-+
| Jamiroquai      | A Funk Odyssey               | Soul           |
| Abba            | Abbas Greatest Hits          | Seventies      |
| Various         | Now 49                       | Pop            |
| westlife        | westlife                     | Pop            |
| Various         | Eurovision Song contest 2001 | Easy Listening |
+—————————————————————–+

The problem with this is, once more, that we already have a fairly complex WHERE clause just to join ALL the records properly. Ideally what we want is to have a LEFT / RIGHT JOIN, but this is a problem as we cannot compare just two tables (Right vs Left). The solution to this is to use a series of joins. This is where one join is made, but rather than being used to evaluate and display the data from it, the result is passed to a second join and THEN the data can be displayed. A good way to think of this is that the first JOIN creates a virtual table (from joining tables one and two) which can then be joined to the third table.

So let us first make a LEFT JOIN between the ‘cds’ and ‘genres’ tables.

mysql> SELECT cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID;
+——————————+—————-+
| title                        | genre          |
+——————————+—————-+
| A Funk Odyssey               | Soul           |
| Abbas Greatest Hits          | Seventies      |
| Now 49                       | Pop            |
| westlife                     | Pop            |
| Eurovision Song contest 2001 | Easy Listening |
+——————————+—————-+
5 rows in set (0.02 sec)

Next we add another JOIN and SELECT the artists.Artist as well. The order in which you SELECT COLUMNs determines how your results will look, so we should place artists.Artist first.

mysql> SELECT  artists.Artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> LEFT JOIN artists
-> ON cds.artistID = artists.artistID;
+————+——————————+—————-+
| name       | title                        | genre          |
+————+——————————+—————-+
| Jamiroquai | A Funk Odyssey               | Soul           |
| Various    | Now 49                       | Pop            |
| westlife   | westlife                     | Pop            |
| Various    | Eurovision Song contest 2001 | Easy Listening |
| Abba       | Abbas Greatest Hits          | Seventies      |
+————+——————————+—————-+
5 rows in set (0.01 sec)

We can now add a WHERE clause to restrict the output.

mysql> SELECT artists.Artist, cds.title, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> LEFT JOIN artists
-> ON cds.artistID = artists.artistID
-> WHERE (genres.genre = ‘Pop’);
+———-+———-+——-+
| name     | title    | genre |
+———-+———-+——-+
| Various  | Now 49   | Pop   |
| westlife | westlife | Pop   |
+———-+———-+——-+
2 rows in set (0.01 sec)

We could also join a fourth table, imagine we were to repeat the steps above to create a ‘label’ table. We could issue the a statement to join all four tables and display the results.

mysql> SELECT artists.Artist, cds.title, label.Label, genres.genre
-> FROM cds
-> LEFT JOIN genres
-> ON cds.genreID = genres.genreID
-> LEFT JOIN artists
-> ON cds.artistID = artists.artistID
-> LEFT JOIN label
-> ON cds.labelID = label.labelID;
+————+——————————+————+—————-+
| Artist     | title                        | Label      | genre          |
+————+——————————+————+—————-+
| Jamiroquai | A Funk Odyssey               | Sony soho2 | Soul           |
| Various    | Now 49                       | virgin     | Pop            |
| westlife   | westlife                     | jive       | Pop            |
| Various    | Eurovision Song contest 2001 | EMI        | Easy Listening |
| Abba       | Abbas Greatest Hits          | EMI        | Seventies      |
+————+——————————+————+—————-+
5 rows in set (0.01 sec)

This method of adding tables and performing JOINS will work only if one TABLE has all the foreign keys. Joins CAN be more complex mixing types etc, but I don’t think it is necessary to go into that here.
UPDATE and DELETE Joins

Requires MySQL 4.0 or later.

As MySQL 4.0 has been deemed to be stable enough for production use it is worth mentioning some of the functionality that has been added and significantly this includes UPDATE and DELETE joins. To find out the version of MySQL that you have access to you are usually told when you login at the command-line:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2471 to server version: 4.0.12

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

But you can also check from the command-line using a ‘STATUS’ command:

mysql> STATUS
————–
mysql  Ver 12.18 Distrib 4.0.12, for pc-linux (i586)

Connection id:          2563
Current database:
Current user:           kbrown@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ”
Server version:         4.0.12

If you do indeed have access to MySQL 4.0 then you can continue with this workshop.
UPDATE Joins

Before looking at the syntax let’s pause for a moment and think why we would want a to perform a Join as part of a UPDATE statement? Well the answer is to update more than one table using only one statement or indeed to insert the values from one table into another.

The syntax for UPDATE joins is very similar to a normal UPDATE statement only including one of the Joins specified above in much the same way as the select statement did. Thus to use an Equi-Join in the statement, we specify the tables together, what we want to SET and then use a WHERE condition to apply the constraint.

UPDATE <table1>, <table2>
SET <column_name> = ‘new_value’
WHERE <conditions>

For Example let’s temporarily change the values of the ‘A Funk Odyssey’ album so that it was recorded by ‘George Clinton’ (see bio if you’re confused), and called ‘The Funkship Odyssey’ (one of the lesser known works ;-) . Thus we have to update the ‘cds’ (to change the title) and the ‘artists’ table, use one condition to constrain the join (cds.artistID = artists.artistID) and then a final condition to only effect that row (and not all rows).

mysql> UPDATE cds, artists
-> SET
-> cds.title = ‘The Funkship Odyssey’,
-> artists.Artist = ‘George Clinton’
-> WHERE (artists.artistID = cds.artistID)
-> AND (cds.cdID = ‘2′);
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

You can use a SELECT join described above to view how this has changed the values.

mysql> SELECT artists.Artist, cds.title
-> FROM artists
-> LEFT JOIN cds
-> USING (artistID)
-> WHERE (cds.artistID = 1);
+—————-+———————-+
| name           | title                |
+—————-+———————-+
| George Clinton | The Funkship Odyssey |
+—————-+———————-+
1 row in set (0.00 sec)

Of course by replacing ‘Jamiroquai’ in the ‘artists’ table it now means that George has recorded all their albums and since George didn’t record ‘The Funkship Odyssey’ we’d better put things back the way they were. This time we will use a LEFT JOIN to UPDATE the values. In a SELECT statement the LEFT JOIN allowed us to join the tables before applying any constraints and in an UPDATE join this is no different. First we make the LEFT JOIN, then use SET to provide new and finally use the WHERE condition to restrict which rows this effects.

mysql> UPDATE cds->LEFT JOIN artists
-> ON cds.artistID = artists.artistID
-> SET
-> cds.title = ‘A Funk Odyssey’,
-> artists.name = ‘Jamiroquai’
-> WHERE (cds.cdID = ‘2′);
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

The other purpose of the UPDATE join is to set the value of a column in one table to that of a column in another table. This isn’t particularly spectacular as any column value in a table can be set to the value of another column in the same row and as any join results in a ‘big virtual table’ this can be done also.

To illustrate let’s recreate the artist field in the cds table that we deleted above and repopulate the column using an UPDATE join. First modify the cds table:

mysql> ALTER TABLE cds
-> ADD artist VARCHAR(20);

Next set a value of ‘Unknown’ for each row.

mysql> UPDATE cds
-> SET artist = ‘Unknown’;
Query OK, 9 rows affected (0.00 sec)
Rows matched: 9  Changed: 9  Warnings: 0

mysql> SELECT cds.artist, cds.title
-> FROM cds;
+———+——————————+
| artists | title                        |
+———+——————————+
| Unknown | A Funk Odyssey               |
| Unknown | Now 49                       |
| Unknown | westlife                     |
| Unknown | Eurovision Song contest 2001 |
| Unknown | Abbas Greatest Hits          |
+———+——————————+
5 rows in set (0.01 sec)

Next we will use an UPDATE Join to merge with the ‘artists’ table and SET the value of the of cds.artist = artists.Artist. Let’s only do one artist (Jamiroquai again) to see how this works.

mysql> UPDATE cds
-> LEFT JOIN artists
-> USING (artistID)
-> SET cds.artists = artists.Artist
-> WHERE (cds.cdID = ‘2′);
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT cds.artists, cds.title
-> FROM cds;
+————+——————————+
| artists    | title                        |
+————+——————————+
| Jamiroquai | A Funk Odyssey               |
| Unknown    | Now 49                       |
| Unknown    | westlife                     |
| Unknown    | Eurovision Song contest 2001 |
| Unknown    | Abbas Greatest Hits          |
+————+——————————+
5 rows in set (0.01 sec)

We can rerun the query without the final WHERE condition that constrains the row and all the artists will be correctly identified by the join.

mysql> UPDATE cds
-> LEFT JOIN artists
-> USING (artistID)
-> SET cds.artists = artists.name;
Query OK, 8 rows affected (0.02 sec)
Rows matched: 9  Changed: 8  Warnings: 0

mysql> SELECT cds.artists, cds.title
-> FROM cds;
+————-+——————————+
| artists     | title                        |
+————-+——————————+
| Jamiroquai  | A Funk Odyssey               |
| Various     | Now 49                       |
| westlife    | westlife                     |
| Various     | Eurovision Song contest 2001 |
| Abba        | Abbas Greatest Hits          |
+————-+——————————+
5 rows in set (0.01 sec)

Notice: These last two examples have included a USING clause as part of the Join.
DELETE Joins

The final Join that I am going to discuss in this workshop is the DELETE Join. This is pretty much a case of ’same again’ so I’ll only give a quick examples in which I’ll delete Westlife who have an artistID = ‘3′ (if only it were that easy ;->.

To DELETE from just the ‘cds’ table include only the ‘cds’ table between DELETE and FROM (the join is made after FROM so both tables are needed there).

mysql> DELETE cds
-> FROM cds, artists
-> WHERE (cds.artistID = artists.artistID)
-> AND (cds.artistID = ‘3′);

To DELETE from both tables:

mysql> DELETE cds, artist
-> FROM cds, artist
-> WHERE (cds.artistID = artists.artistID)
-> AND (cds.artistID = ‘3′);

javascript validations Numeric data

function isValidPhoneNumbers(evt) {

var charCode = (evt.which) ? evt.which : event.keyCode

if (charCode == 43 || (charCode <57 && charCode> 47)) {

return true;

} else {

return false;

}

}

HTML:

<input onkeypress=”return isValidPhoneNumbers(event)” type=”text” />

javascript popup window

<html>
<head>
<script language=”JavaScript”>
function fnOpenPassWin( obj ){
obj.href = “#”; //Prevent user from navigating to password page in the main window
var passwin = “http://yahoo.co.in“;
var winprop = “width=600,height=500,location=no,toolbar=no,menubar=no,scrollbars=yes”;
var newwin = window.open(passwin, “”, winprop);
}
</script>
</head>

<body>
<a href=”#” onclick=”javascript:fnOpenPassWin(this)”>yahoo.com</a>
</body>
</html>