Google celebrates 8th Anniversary

September 27th, 2006

google
Google Celebrates its 8th Birthday :)

PHP/MySQL Interview Questions

September 14th, 2006

How can we repair a MySQL table?
The syntax for repairing a mysql table is
REPAIR TABLENAME, [TABLENAME, ], [Quick],[Extended]
This command will repair the table specified
if the quick is given the mysql will do a repair of only the index tree if the extended is given it will create index row by row

maximum length of mnames of database, table, columns
database- 64
table -64
columns-64
alias-255

How many values can the SET function of MySQL take?
Mysql set can take zero or more values but at the maximum it can take 64 values
What are the other commands to know the structure of table using MySQL commands except explain command?
describe table_name;

How can we find the number of rows in a table using MySQL? How can we find the number of rows in a result set using PHP?
Use this for mysql
>SELECT COUNT(*) FROM table_name;
but if u r particular about no of rows with some special result
do this
>SELECT [colms],COUNT(*) FROM table_name [where u put conditions];

and for PHP it cant be more simple

$result = mysql_query($any_valid_sql, $database_link);
$num_rows = mysql_num_rows($result);
echo “$num_rows rows found”;
#

How many ways we can we find the current date using MySQL?
SELECT CURDATE();
CURRENT_DATE() = CURDATE()
for time use
SELECT CURTIME();
CURRENT_TIME() = CURTIME()

What is the difference between char and varchar data types?
Set char to occupy n bytes and it will take n bytes even if u r storing avalue of n-m butes
Set varchar to occupy n bytes and it will take only the required space and will not use the n bytes
eg. name char(10) will waste 5 bytes if we store ‘kumar’, if each char takes a byte
eg. name varchar(10) will just use 5 bytes if we store ‘kumar’, if each char takes a byte. rest 5 bytes will be free.

What is the functionality of md5 function in PHP?
string md5(string)
Calculate the md5 hash of a string. The hash is a 32-character hexadecimal number. I use it to generate keys which I use to identify users etc. If I add random no techniques to it the md5 generated now will be totally different for the same string I am using.

How can I load data from a text file into a table?
The mysql provides a LOAD DATA INFILE syntax. U can load data from a file. Gr8 tool but u need to make sure that
a) data is delimited
b) u match the colms and data correctly
dont use w/out first learning the syntax

How can we know the number of days between two given dates using MySQL?
Use DATEDIFF()
>SELECT DATEDIFF(NOW(),’1947-08-15′);
will give u the exact no of days India got independence from British.

How can we know the number of days between two given dates using PHP?
Simple arithmetic.
$date1 = date(”Y-m-d”);
$date2 = “1947-08-15″;
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo “No of $days we got independence from Britts”;

How can we change the name of a column of a table?
How can we change the name and data type of a column of a table?
this will change the name of colm
> ALTER TABLE table_name CHANGE old_colm_name new_colm_name
this will change the name of colm and also the datatype
> ALTER TABLE table_name CHANGE old_colm_name new_colm_name [data type]

What are the differences between drop a table and truncate a table?
DROP TABLE table_name
Will DELETE the table and DATA
TRUNCATE TABLE table_name
Will DELETE the table DATA not the table definition

What is PEAR in PHP
PEAR is short for “PHP Extension and Application Repository” and is pronounced just like the fruit. The purpose of PEAR is to provide:

  • A structured library of open-sourced code for PHP users
  • A system for code distribution and package maintenance
  • A standard style for code written in PHP
  • The PHP Foundation Classes (PFC),
  • The PHP Extension Community Library (PECL),

A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.

http://pear.php.net/manual/en/introduction.php

What is the difference between GROUP BY and ORDER BY in Sql?
Group by which is used to sort a table interms of colums order by which is used to index a table by colum value
ORDER BY [col1],[col2],…,[coln]; Tels DBMS according to what columns it should sort the result. If two rows will hawe the same value in col1 it will try to sort them according to col2 and so on.
GROUP BY [col1],[col2],…,[coln]; Tels DBMS to group results with same value of column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if you want to count all items in group, sum all values or view average

What is MIME?
MIME is Multipurpose Internet Mail Extensions is an internet standard for the format of e-mail. Howewer browsers also uses MIME standart to transmit files. MIME has a header wich is added to a begining of the data. When browser sees such header it shows the data as it would be a file (for example image)
some mimes:
audio/x-ms-wmp
image/png
aplication/x-shockwave-flash

How can we know that a session is started or not?
a session starts by session_start()function.
this session_start() is always declared in header portion.it always declares first.then we write session_register().

What are the differences between MySQL_fetch_array(), MySQL_fetch_object(), MySQL_fetch_row()?
Ans:- MySQL_fetch_array()->Fetch a result row as an associative array,numeric array.
MySQL_fetch_object()->Fetch a result row as an object.
MySQL_fetch_row()->Fetch a result set as an array().

If we login more than one browser window at the same time with same user and after that we close one window then is the session is exist to other window or not.And if yes then why? or if no then why?
If we login more than one browser window at the same time with same user and after that we close one window then is the session is exist to other window or not.And if yes then why? or if no then why?
session depends on browser. if browser is closed then session is lost. the session data will be deleted after session time out. if connection is lost and u recreate connection, then also sesssion will continue in the browser.

What are the database files stored in system?
data’s are stored in name.Myd
table Structure are name.$frm
Index tables are name.myi

Difference between php4 and php5?
php4 cannot support oops concepts and zend engine1 will be used
in php5 support oops concepts and zends engine2 will be used
error supporting will increased in php5
xml and sqllite will be incresed in php5

CVS or RCS

September 7th, 2006

CVS actually uses RCS underneath. CVS is a lot more powerful tool and can control a complete source code tree. It is very strongly recommended that you use CVS, because you can greatly customize CVS with scripting languages like PERL, Korn and bash shells.

Advantages of CVS:

* CVS is decentralized so a user checks out files/directories from the repository and have his own separate stable source directory tree.
* CVS can “STAMP” releases of an entire project source tree.
* CVS can enable concurrent editing of files.
* CVS can be greatly customized to enable strong locking of files via shell scripts or PERL scripts. CVS supports weak locking with the command ‘cvs watches’ and also no locking permitting concurrent editing of files.

Disadvantages of CVS:

* Needs a little more administration than RCS.
* Very highly sophisticated and complex system. It is “State of the Art” technology. The cvs software is a very advanced and capable system developed over a very long period of time (it took several years!!). It took about 20 to 30 years of research to develop CVS and it is still evolving!!
* Has a large number of commands and command options, hence a steeper learning curve for beginners. The shell scripts at Shell Scripts can ease usage.

Advantages of RCS:

* RCS is very simple to setup, with less administrative work.
* RCS is used in a centralized area where everyone works.
* RCS is useful for simple systems.
* Very strong locking of files - concurrency eliminated.

Downside of RCS:

* Concurrent development by multiple developers is not possible due to file locking and being limited to a single working directory. Because of the single working directory limitation, changes to files by multiple developers can cause failure of the ‘make’ command.
* Cannot stamp releases of an entire software project.

Strict — Perl Pragma

September 7th, 2006

1. strict refs

This generates a runtime error if you use symbolic references
use strict ‘refs’;
$ref = \$foo;
print $ref; # ok
$ref = “foo”;
print $ref; # runtime error; normally ok
$file = “STDOUT”;
print $file “Hi!”; # error; note: no comma after $file

There is one exception to this rule:
$bar = \&{’foo’};
&$bar;
is allowed so that goto &$AUTOLOAD would not break under stricture.

strict vars

This generates a compile-time error if you access a variable that wasn’t declared via our or use vars , localized via my(), or wasn’t fully qualified.Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local() variable isn’t good enough. See my and local.
use strict ‘vars’;
$X::foo = 1; # ok, fully qualified
my $foo = 10; # ok, my() var
local $foo = 9; # blows up

package Cinna;
our $bar; # Declares $bar in current package
$bar = ‘HgS’; # ok, global declared via pragma

The local() generated a compile-time error because you just touched a global name without fully qualifying it.

Because of their special use by sort(), the variables $a and $b are exempted from this check.

strict subs

This disables the poetry optimization, generating a compile-time error if you try to use a bareword identifier that’s not a subroutine, unless it is a simple identifier (no colons) and that it appears in curly braces or on the left hand side of the => symbol.

use strict ’subs’;
$SIG{PIPE} = Plumber; # blows up
$SIG{PIPE} = “Plumber”; # just fine: quoted string is always ok
$SIG{PIPE} = \&Plumber; # preferred form

Single Quotes vs Double Quotes in PHP

August 1st, 2006

Most of us use only Double Quotes ['’] when writing PHP code. Usage of single quotes is better than using Double Quotes

Instead of :

<?php echo “Visit http://www.weberblog.com/“;?>

You can Use :

<?php echo ‘Visit http://www.weberblog.com‘;?>

In the above example there is no difference but have a look at this example :

Double Quotes way :

<?php echo ” <table cellspacing=”\” cellpadding=”\” border=”\”>”;?>

Single Quotes way :

<?php echo ‘<table cellspacing=”0″ cellpadding=”0″ border=”1″>’; ?>

In this example you can see that we don’t need to add escape characters for each Double Quote we want to echo as HTML. This, by its self is a good enough reason to migrate, however, a 2nd reason is performance. Using Single Quotes is always at least as fast as Double Quotes and in some cases faster by hundreds of percents.

Conclusion:Single and double quoted strings behave almost the same with one exception: Don’t use the a lonely ($) in double quoted string unless you want to reference a PHP-var; or use (\$).

Note : When working with special formatting characters such as \n \r \t etc… Single Quotes are somewhat problematic because they ignore these formatting codes. What you need to do in case you need these formatting codes is :

<?php echo ‘check out http://www.weberblog.com . “\n\r” . ‘for the best PHP, MySQL Web Logs on the net’;?>