Monthly archive for December 2009

How to find nth highest salary

December 28, 2009 | Filed Under PHP | Comments Off

To find nth highest salary use below code.

SELECT * FROM emp employee1 WHERE n = (SELECT COUNT(DISTINCT (employee2.salary) FROM emp employee2 WHERE employee2.salary >= employee1.salary))

use find 2th highest salary put only 2 on the place of n on the above query.
the query will become

SELECT  * FROM emp employee1 WHERE 2 = (SELECT COUNT(DISTINCT (employee2.salary) FROM emp employee2 WHERE employee2.salary >= employee1.salary))

for 3,4,5 ……, put exact number on the place of n on the above query.

Article written by admin

How to find the second highest salary from emp table?

December 28, 2009 | Filed Under MySQL | Comments Off

There are number of examples to get second highest salary from EMP table.

example-select sal from(select sal from
(select  distinct sal from emp order by sal desc)
where rownum<=2 order by sal asc)
where rownum=1;

example- SELECT MAX(SAL) FROM EMP WHERE SAL NOT IN (SELECT MAX(SAL)  FROM EMP)

where EMP is table name,SAL is salary colum.

Article written by admin

What is the use of #id selector in JQuery library.

December 28, 2009 | Filed Under PHP | Comments Off

ID attribute play very important rule in web page.
In a web page every web element must have unique id.

Ex-<input type=”text” name=”text1” id=”text1”/>
<input type=”text” name=”text2” id=”text2”/>

To  get value of above  textbox from #id selector using jquery need to write below code.

var value=$(“# text1”).val();

another example, To apply css on #myDivId element use below code.

$(“#myDivId”).css(“border”,”3px solid red”);

Article written by admin

In how many ways we can use ajax in JQuery library

December 28, 2009 | Filed Under PHP | Comments Off

There are number of ways to use Ajax in JQuery library

GET Oriented Method:
If you want Ajax activity either on load page, submit form, paging what ever. It depend on you
Use below code


$. get (“back.php”, {name1: “php”, name2: “interview”},
function(data){
alert(“Data Loaded: ” + data);
});

Where
back.php = back file need to call.
name1: “php”, name1: “interview” = values to send on back.php.
data = value which back.php will return.

Please see look on back.php

POST Oriented Method:

Please changes $.get to $.post in above code.

Third method:

This is third method to use Ajax using JQuery library.

$.ajax({
type: “POST”,
Url: “back.php”,
data: “name=php&location=India”,
success:function (msg){
alert( “Data Saved: ” + msg);
}
});

Article written by admin

Set Timezone in php.

December 26, 2009 | Filed Under PHP | 1 Comment

Hello Visitor
Here is a example how we can set time zone in php.Time zone functionally is very important in auction sites, freelance based sites, product base sites, but many programmer don’t care about time zone.
due to this lake of programming visitor face problems and sites goes to down.
Here is an example how we can solve time zone issue according to
PHP script.

Example - Suppose you are from India and you submit a product in auction bases site, you insert end date of product according to Indian time zone but server is in UK, then automatically the end date of product will be on UK time zone( insert in mysql). but it must be show for you in Indian time zone, anybody viewing this product will show on their < country own time zone not UK based data and time.

So for this PHP Provide a solution.

1-Create a table

CREATE TABLE IF NOT EXISTS `timezone` (
`timezid` int(11) NOT NULL auto_increment,
`tz` varchar(250) NOT NULL default ”,
`gmt` text NOT NULL,
PRIMARY KEY (`timezid`)
) ENGINE=InnoDB ;

INSERT INTO `timezone` (`timezid`, `tz`, `gmt`) VALUES
(1, ‘Pacific/Kwajalein’, ‘(GMT -12:00) Eniwetok, Kwajalein’),
(2, ‘Pacific/Samoa’, ‘(GMT -11:00) Midway Island, Samoa’),
(3, ‘Pacific/Honolulu’, ‘(GMT -10:00) Hawaii’),
(4, ‘America/Anchorage’, ‘(GMT -9:00) Alaska’),
(5, ‘America/Los_Angeles’, ‘(GMT -8:00) Pacific Time (US & Canada) Los Angeles, Seattle’),
(6, ‘America/Denver’, ‘(GMT -7:00) Mountain Time (US & Canada) Denver’),
(7, ‘America/Chicago’, ‘(GMT -6:00) Central Time (US & Canada), Chicago, Mexico City’),
(8, ‘America/New_York’, ‘(GMT -5:00) Eastern Time (US & Canada), New York, Bogota, Lima’),
(9, ‘Atlantic/Bermuda’, ‘(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz’),
(10, ‘Canada/Newfoundland’, ‘(GMT -3:30) Newfoundland’),
(11, ‘Brazil/East’, ‘(GMT -3:00) Brazil, Buenos Aires, Georgetown’),
(12, ‘Atlantic/Azores’, ‘(GMT -2:00) Mid-Atlantic’),
(13, ‘Atlantic/Cape_Verde’, ‘(GMT -1:00 hour) Azores, Cape Verde Islands’),
(14, ‘Europe/London’, ‘(GMT) Western Europe Time, London, Lisbon, Casablanca’),
(15, ‘Europe/Brussels’, ‘(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris’),
(16, ‘Europe/Helsinki’, ‘(GMT +2:00) Kaliningrad, South Africa’),
(17, ‘Asia/Baghdad’, ‘(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg’),
(18, ‘Asia/Tehran’, ‘(GMT +3:30) Tehran’),
(19, ‘Asia/Baku’, ‘(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi’),
(20, ‘Asia/Kabul’, ‘(GMT +4:30) Kabul’),
(21, ‘Asia/Karachi’, ‘(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent’),
(22, ‘Asia/Calcutta’, ‘(GMT +5:30) Bombay, Calcutta, Madras, New Delhi’),
(23, ‘Asia/Dhaka’, ‘(GMT +6:00) Almaty, Dhaka, Colombo’),
(24, ‘Asia/Bangkok’, ‘(GMT +7:00) Bangkok, Hanoi, Jakarta’),
(25, ‘Asia/Hong_Kong’, ‘(GMT +8:00) Beijing, Perth, Singapore, Hong Kong’),
(26, ‘Asia/Tokyo’, ‘(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk’),
(27, ‘Australia/Adelaide’, ‘(GMT +9:30) Adelaide, Darwin’),
(28, ‘Pacific/Guam’, ‘(GMT +10:00) Eastern Australia, Guam, Vladivostok’),
(29, ‘Asia/Magadan’, ‘(GMT +11:00) Magadan, Solomon Islands, New Caledonia’),
(30, ‘Pacific/Fiji’, ‘(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka’);

This table contain all countries timezone parameter.

2-PHP function

function getCorrectTime($date_time_string, $tz, $date_format = “r”) {

$time = strtotime($date_time_string);

$tz_bak = getenv(“TZ”);

putenv(“TZ=$tz”);

$validdate= date($date_time_string, $time);

putenv(“TZ” . ($tz_bak ? “=$tz_bak” : “”));

return $validdate;

}

where
$date_time_string is inserted date/datetime in database.

$tz is the time zone value from database like Asia/Calcutta for INDIA

$date_format is date/datetime format in which format you want.

Usage

$date=’2009-06-29 11:02′ // value from data base

$timezone=’Asia/Calcutta’; // you want indian time zone

$format=’F d,Y H:i a’ // give format of date and time in which format you want date and time
getCorrectTime($date,$timezone,$format);

Article written by admin

File reading/downloading using php.

December 23, 2009 | Filed Under PHP | Comments Off

This is the example, how to download pdf file using php. you can also use this code for zip file,word file,image file, any type of media file.

$filename=”directory/filename”;//In $filename will have to  provide full path of file.

header(“Pragma: public”);

header(“Expires: 0″);

header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);

header(“Content-Type: application/force-download”);

header(“Content-Type: application/octet-stream”);

header(“Content-Type: application/download”);

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);

header(“Content-Transfer-Encoding: binary”);

header(“Content-type: application/pdf”);

header(“Content-Length: “.filesize($filename));

readfile(“$filename”);

exit();

If your file is pdf then use header(“Content-type: application/pdf”);

If zip file then use  header(“Content-type: application/zip”);

In $filename provide full path of file.

Ex. If your file is in  pdf folder and your php file is in  outside of pdf folder.

Then $filename=”pdf/filename.pdf”;//  will be.

If you want to display file not download, You have to remove below header  from above code.

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);


Article written by admin

Difference between include_once() and require_once().

December 19, 2009 | Filed Under PHP | Comments Off

Both are  used to includes files once. Means PHP will check if the file has already been included, and if so, not include (include) it again.

include _once() create  warning  if file not found and  execute   script.

require_once() create fatal  error  if file not found and  terminate  script.

Article written by admin

What difference between require() and require_once().

December 19, 2009 | Filed Under PHP | 1 Comment

Require()

The  Require() is  used to include a file, It create fatal  error if file not found and  terminate  script.

require_once()

The require_once() to require() except PHP will check if the file has already been included, and  if so, tricor online not include (require) it again.

Article written by admin

What difference between include () and include_once()?

December 19, 2009 | Filed Under PHP | Comments Off

include ()

The include ()  is  used to include a file, It create warning  if file not found and  execute   script.

include_once()

The include _once() statement include () except PHP will check if the file has already been included, and if so, not include (include) it again.

Article written by admin

How many ways can includes files in php.

December 19, 2009 | Filed Under PHP | Comments Off

There are  4 methods to includes  files

require()

require_once()

include()

include_once()

Article written by admin

Superglobal variables in php.

December 19, 2009 | Filed Under PHP | Comments Off

There are  number of superglobal variables in php

  • $GLOBALS
  • $_SERVER
  • $_GET
  • $_POST
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV
Article written by admin

PHP cURL functions tutorial

December 19, 2009 | Filed Under PHP | 1 Comment

cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files  from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps.

curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_exec – Executes a cURL session.

curl_close – Closes the current cURL session.

Below are some examples which should make the working of cURL more clearer.

The below piece of PHP code uses cURL to download Google’s RSS feed.

<?php
/**
* Initialize the cURL session
*/

$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/

curl_setopt($ch, CURLOPT_URL,
‘http://news.google.com/news?hl=en&topic=t&output=rss’);
/**
* Ask cURL to return the contents in a variable
* instead of simply echoing them to the browser.
*/

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/**
* Execute the cURL session
*/

$contents = curl_exec ($ch);
/**
* Close cURL session
*/

curl_close ($ch);
?>

As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.

The above code uses two such options.

  • CURLOPT_URL: Use it  to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
  • CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the contents instead of echoing them to the browser.
Article written by tabrez

What is Type Juggling?

December 19, 2009 | Filed Under PHP | Comments Off

Juggling means

throwing and catching several objects simultaneously.

Type Juggling in php  means automatically type conversion.

That is to say, if a string value is assigned to variable $var$var becomes a string. If an integer  value is assigned to $var, it becomes an integer.

Please see Example.

<?php
$var = "0";// $var is string (ASCII 48)
$var += 10; // $var is now an integer (10)
$var = $foo+ 1.5; // $var is now a float (11.5)
$var = 5 + "10 Little hots"; // $var  is integer (15)
$ var = 5 + "10 Small hots"; // $var  is integer (15)
?>

$var variable comes many times in different data type.

Often it became interger, float.

This is Type Juggling in php.

Thank you

Article written by admin

How many data type are present in PHP.

December 18, 2009 | Filed Under PHP | Comments Off

PHP supports 8  data  types.

  • boolean
  • integer
  • float (floating-point number, aka double)
  • string
  • array
  • object
  • resource
  • NULL

Explaination:

4 scalar types:

boolean

<?php

$var = True; // assign the value TRUE to $foo
?>

integer

<?php
$var = 123; // decimal number
$var = -122; // a negative number
$var = 0111; // octal number
$var = 0x1B; // hexadecimal number

?>

float (floating-point number, aka double)

<?php
$var = 1.234;
$var= 1.2e3;
$var = 7E-10;
?>

string

<?php
$string =”Hello World”;  // Assign Hello World into $string

?>

2 compound types:

array

<?php
$array = array(“first”=>”Hello”,”second”=>”World”);

echo $array[‘first’];

echo $array[‘second];

?>

object

<?php

Class Interview

{

Function php()

{

Echo “What  is php”;

}

}

$obj = new Interview(); // $obj is object , you can also take different name   like  $object,$interview

Echo $obj-> php();

?>

3 compound types:

resource

NULL

<?php
$variable = NULL;
?>

Note:  PHP does not require to define variable in in any specific data type. Like  integer,float,string etc, like C,C++.

PHP  support   automatic type conversion .
For More Detail see Type Juggling articles.

Thank you

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker