Skip to main content

PHP & Mysql interview question

What’s 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

Who is the father of PHP and explain the changes in PHP versions?
 Rasmus Lerdorf in 1994

 How many ways you can retrieve the data in the result set of mysql using PHP?
We can do it by 4 Ways
         1. mysql_fetch_row ,
               The mysql_fetch_row() function fetches one row from a result-set and returns it as an
               enumerated array.
        2. mysql_fetch_array ,
               The mysql_fetch_array() function fetches a result row as an associative array, a numeric
                array, or both.
        3. mysql_fetch_object
               The mysql_fetch_object() function returns the current row of a result set, as an object.
         4. mysql_fetch_assoc
                The mysql_fetch_assoc() function fetches a result row as an associative array.

 How can we create a database using php and mysql?
  <?php $query  =mysql_query( "CREATE DATABASE phpcake");?>

 What is the difference between mysql_fetch_object and mysql_fetch_array?
 1. object is returned, instead of an array
         2. In this you can only access the data by the field names

 What are the differences between Get and post methods.
  •   GET requests can be cached
  •   GET requests remain in the browser history
  •   GET requests can be bookmarked
  •   GET requests should never be used when dealing with sensitive data
  •   GET requests have length restrictions
  •   GET requests should be used only to retrieve data

  •   POST requests are never cached
  •   POST requests do not remain in the browser history
  •   POST requests cannot be bookmarked
  •   POST requests have no restrictions on data length

  GET Method have some limit like only 2Kb data able to send for request But
  in POST method unlimited data can we send.

 What are the differences between require and include?
 Both does the same functionality. It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

require will produce a fatal error (E_COMPILE_ERROR) and stop the script
include will only produce a warning (E_WARNING) and the script will continue


 What is the difference between the functions unlink and unset?
         unlink() deletes the given file from the file system.
         unset()   makes a variable undefined.

 What are the different functions in sorting an array?
          Sort()  ----  Sort an array
          asort() ---   Sort an array and maintain index association
          arsort()  --- Sort an array in reverse order and maintain index association
          ksort()  --- Sort an array by key
          natsort(), natcasesort(),rsort(), usort(),array_multisort(), and uksort().

 How can we know the count/number of elements of an array?
          a) sizeof($urarray) This function is an alias of count()
          B) count($urarray)

 How can we find the number of rows in a table using MySQL?
   SELECT COUNT(*) FROM table_name;

 How can we know the number of days between two given dates using PHP?
        $date1 = date("Y-m-d");
        $date2 = "2006-08-15";
        $days = (strtotime($date1) - strtotime($date2)) / (60 * 60 * 24);

 what are magic methods?
 Magic methods are the members functions that is available to all the instance of class Magic methods always starts with "__". Eg. __construct All magic methods needs to be declared as public To use magic method they should be defined within the class or program scope Various Magic Methods used in PHP 5 are: __construct() __destruct() __set() __get() __call() __toString() __sleep() __wakeup() __isset() __unset() __autoload() __clone()

  What Is a Session?A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.
There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.
Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

What is meant by PEAR in php?
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP.  PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install “packages”

Explain the ternary conditional operator in PHP?
Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.

 How to get the value of current session id?
session_id() returns the session id for the current session.

How can we know the number of days between two given dates using PHP?
Simple arithmetic:
$date1 = date(‘Y-m-d’);
$date2 = ’2006-07-01′;
$days = (strtotime() – strtotime()) / (60 * 60 * 24);
echo “Number of days since ’2006-07-01′: $days”;

How can we repair a MySQL table?

REPAIR TABLE tablename
REPAIR TABLE tablename QUICK
REPAIR TABLE tablename EXTENDED
This command will repair the table specified.
If QUICK is given, MySQL will do a repair of only the index tree.
If EXTENDED is given, it will create index row by row.


What is the difference between $message and $$message?
 $message is a simple variable whereas $$message is a reference variable. Example:
 $user = ‘bob’        
is equivalent to
 $holder = ‘user’;
 $$holder = ‘bob’;

What Is a Persistent Cookie?
A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:

    Temporary cookies can not be used for tracking long-term information.
    Persistent cookies can be used for tracking long-term information.
    Temporary cookies are safer because no programs other than the browser can access them.
    Persistent cookies are less secure because users can open cookie files see the cookie values.

What does a special set of tags do in PHP?
The output is displayed directly to the browser.

How do you define a constant?
Via define() directive, like define (“MYCONSTANT”, 100);

What are the differences between require and include, include_once?

require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.
But require() and include() will do it as many times they are asked to do.

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. The major difference between include() and require() is that in failure include() produces a warning message whereas require() produces a fatal errors.


What is meant by urlencode and urldecode?

urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode("10.00%") will return "10%2E00%25". URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.


How To Get the Uploaded File Information in the Receiving Script?
Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This receiving PHP script can get the uploaded file information through the predefined array called $_FILES. Uploaded file information is organized in $_FILES as a two-dimensional array as:


    $_FILES[$fieldName]['name'] - The Original file name on the browser system.
    $_FILES[$fieldName]['type'] – The file type determined by the browser.
    $_FILES[$fieldName]['size'] – The Number of bytes of the file content.
    $_FILES[$fieldName]['tmp_name'] – The temporary filename of the file in which the uploaded
                                                                  file was stored on the server.
    $_FILES[$fieldName]['error'] – The error code associated with this file upload.
The $fieldName is the name used in the &lt;INPUT TYPE=FILE, NAME=fieldName&gt;

What is the difference between mysql_fetch_object and mysql_fetch_array?
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array

How can I execute a PHP script using command line?
Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, "php myScript.php", assuming "php" is the command to invoke the CLI program.

Comments

Popular posts from this blog

How to Create a PDF file in Cakephp 2.0 using Fpdf

Step 1: Download FPDF folder from  http://www.fpdf.org/  . Step 2: Unzip the downloaded Fpdf file and name it “fpdf” or however you require and make sure that you use the same name while calling it. Step 3: Move the “fpdf” unzipped files to  your /app/Vendor directory within Cakephp. Now you should have the directory path as   /app/Vendor/fpdf. Step 4: Create a new Cakephp layout file for the pdfs. We will use this layout when serving a pdf to the client. Create a file called pdf.ctp inside of /app/View/Layouts. Add the following code to /app/View/Layouts/pdf.ctp Layout: /app/View/Layouts/pdf.ctp 1 2 3 4 <?php      header ( 'Content-Disposition: attachment; filename="downloaded.pdf"' ) ;      echo $content_for_layout ; ?> The header function above tells the browser that it is going to receive a file called download.pdf. If you want to change the name

Setup CakePHP Using Xampp On Windows

Step 1: Download XAMPP  and  CakePHP .   Step 2: Install Xampp Once you have installed Xampp (version 1.7.3) on your Windows with the default option, all your files will be located in the C:\xampp folder. Step 3: Mod Rewrite Module Once Xampp is installed as the local server, you can then proceed to enable mod_rewrite. To do so, you will have to open the httpd.conf file that is located in C:\xampp\apache\conf and uncomment by removing # from the following line: # LoadModule rewrite_module modules/mod_rewrite.so Step 4: Place CakePHP Files in a New Folder Extract the CakePHP (version 1.3.8) zip file and copy all its contents to your local web server, which in this instance is C:\xampp\htdocs\cakephp . I have decided to name the CakePHP folder as cakephp, and in it, you will find many files and folders for the framework, including app, cake, docs, vendors, .htaccess, and index.php. Step 5: Set Up Virtual Host Open the httpd-vhosts.conf file from the C:\xampp\apa

Installing Wamp on Windows

WAMP is an abbreviated name for the software stack Windows, Apache, MySQL, PHP. It is  derived from LAMP which stands for Linux, Apache, MySQL, and PHP. As the name implies, while LAMP is used on Linux servers, WAMP is used on Windows servers.  The “A” in WAMP stands for Apache.  Apache  is server software that is used to serve webpages. Whenever someone types in your WordPress website’s URL, Apache is the software that “serves” your WordPress site. The “M” in WAMP stands for MySQL.  MySQL  is a database management system. It’s job in the software stack is to store all of your website’s content, user profiles, comments, etc. The “P” in WAMP stands for PHP. PHP is the programming language that WordPress is written in. It is also the piece that holds the entire software stack together. It runs as a process in Apache and communicates with the MySQL database to dynamically build your webpages. Download the wamp for the url  http://www.wampserver.com/en/download.php .  You

Dynamic Sitemap Generation plugin in Cakephp

Here for the SEO implementation we need to generate the sitemap.xml in our application which is accessed by the webmaster tool. So here i am outlined the steps to generate the Xml file . 1. Lets think we have controller by name sitemap,Inside that create an action by name sitemap and paste the following code    public function sitemap(){     $this->layout='ajax';     $this->RequestHandler->respondAs('xml');     $listData = $this->Sitemap->find('all',/*Conditions if you have any*/);     $this->set(compact('listData')); } I through variable $listData to render all data( Keywords,Title,Url,etc ...) that will be shown in sitemap.xml.  This   depends   on the   dynamic link  what   we want to   show  in sitemap.xml.For request handler to work include the RequestHandler component public $components = array('RequestHandler'); Step 2. Create View based on action sitemap On structure MVC as default we need create sitemap.