Entries by admin

What is difference between Persistent Cookie and Temporary cookies?

January 10, 2010 | Filed Under PHP | 2 Comments

Temporary cookies :

By Default Temporary cookie is active which is store in browser memory, live until browser  is closed.

Persistent Cookie: persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer.

Which is live for a time period defines by programmer in the script.

Example:

<?php

$value = ‘value’;
setcookie("testcookie", $value, time()+3600); /* expire in 1 hour */

// Where 3600 count in 60*60*1 [second*minute*hour]

?>

Article written by admin

Days between two given dates using PHP?

January 10, 2010 | Filed Under PHP | Comments Off

$date1 = date(‘Y-m-d’); // yyyy-mm-dd  format
$date2 = ’2006-07-01′;
$days = (strtotime() – strtotime()) / (60 * 60 * 24);
echo “Number of days since ’2006-07-01′: $days”;

Where strtotime

int strtotime ( string $time [, int $now] )

The function expects to be given  a string containing a US English date format and will try to parse that format into a UNIX timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or the current time if now is not supplied.

Article written by admin

How can we repair a MySQL table?

January 10, 2010 | Filed Under JQuery, PHP | Comments Off

There are three syntax  to repair MySQL table.

REPAIR TABLE table_name
REPAIR TABLE table_name QUICK
REPAIR TABLE table_name EXTENDED

If QUICK is given, MySQL will repair index tree.
If EXTENDED is given, it will create index row by row.

Article written by admin

what is LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN in mysql?

January 10, 2010 | Filed Under MySQL, PHP | Comments Off

The best way to learn about JOINS is the example. So below is the example to demonstrate JOINS
Suppose that there are two tables
1- Person
2-Information
Person table has definition and data like  that
Person:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

Information:

border=”1″ width=”400″ cellspacing=”0″ cellpadding=”10″>

Entry_id Number Address
1 1 ChandPura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

mysql> select * from person;

out put:

Number(Primary key) name phone
1 Mr Sana 0120 234564
2 Mr Urooj 0120 234567
3 Mr Imran 0120 245678

3 rows in set (0.00 sec)

mysql> select * from information;

Entry_id Number Address
1 1 Chand Pura, Bahraich UP (INDIA)
2 3 Dadi hat, Bahraich UP (INDIA)
3 3 Qazi pura, Bahraich UP (INDIA)
4 3 New Ashok Nagar, Delhi UP (INDIA)
5 4 Faizadab UP (INDIA)

5 rows in set (0.00 sec)

Regular Join:

If apply a regular JOIN (with out use of these 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, address

from person join information

on person.number = information.number;

OR

mysql> select name, phone, address

from person,information where

person.number = information.number;

Output:

name Phone Address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP (INDIA)

4 rows in set (0.01 sec)

LEFT JOIN:
If I apply left joint with using keyword LEFT JOIN

mysql> select name, phone, address
from person left join information on person.number = information.number;

Output:

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Urooj 0120 234567 NULL
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP

5 rows in set (0.00 sec)

RIGHT JOIN:
If I apply right joint with using keyword RIGHT JOIN

mysql> select name, phone, address
from person right join information on person.number = information.number;

name phone address
Mr Sana 0120 234564 Chand Pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 Dadi hat, Bahraich UP (INDIA)
Mr Imran 0120 245678 Qazi pura, Bahraich UP (INDIA)
Mr Imran 0120 245678 New Ashok Nagar, Delhi UP
NULL NULL Faizadab UP (INDIA)

5 rows in set (0.00 sec)

OUTER JOIN: Outer join does not support RDBMS.

Article written by admin

What is .htaccess in apache?

January 5, 2010 | Filed Under .htaccess | Comments Off

The .htaccess is Apache configuration file.which is used to control many feature on Apache webserver.
Features are follows

  • password protect directories
  • enable server side includes
  • block users by IP address among other things
  • Create  meaning full URL
  • Change in php.ini settings
  • Forbidding Files
  • Checking access rights
  • show different pages depending on the IP address:
  • process Apache errors (403,404,405)

and many more.

The file name is exactly “dot” htaccess. It must be in the UNIX format (ASCII mode).

Article written by admin

How to enable URL file-access in the server configuration or Godaddy server

January 5, 2010 | Filed Under Others | 5 Comments

If you are getting Warning Messages like this one

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/xxxxxxxxxxxxxxxxxxxxxxxxxx/filecall.php on line 5

Where xxxxxxxxxxxxxxxxxxxxxxxxxx < your server path.

It ‘s means that your server configuration is blocking external file access( include () function in many cases).

To solve this issue you need to set allow_url_fopen= ON and allow_url_include =ON in PHP configuration (php.ini).

If you have not dedicated server, You need to ask for your service provider to set allow_url_fopen= ON, allow_url_include =ON

If you want enable URL file-access setting=on via .htacess
then you need to write php_value allow_url_fopen 1 in .htacess file.
If you want to enable URL file-access via PHP.
Then you need to write <?php
ini_set(‘allow_url_fopen’,'ON’) ; // ON or 1 both are same
ini_set(‘allow_url_fopen’,'ON’) ;
?>

I suggest a solution simple use file_get_contents()instead of include() function.
In this case you don’t need to enable URL file-accessin the server configuration.

Basically Godaddy server doesn’t support include() function due to disable allow_url_fopen.

Article written by admin

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

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