PHP and MySql Interview Question Paper with Answer

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.
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.

Write a simple code to know the number of days between two given dates using PHP?
$date1 = date(‘Y-m-d’);

$date2 = ‘2006-07-01’;

$days = (strtotime() – strtotime()) / (60 * 60 * 24);

echo “Number of days since ‘2006-07-01’: $days”;

What does a special set of tags <?= and ?> 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 the differences are between require and  include
The major difference between include () and require () is that in failure include() produces a warning message whereas require() produces a fatal errors.
What the differences are between include  and  include_once
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.

Include () will do it as many times they are asked to do.

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
Write a code How To Create a Table?
If you want to create a table, you can run the CREATE TABLE statement as shown in the following sample script:

<?php

include “mysql_connection.php”;

$sql = “CREATE TABLE Tech_links (”

. ” id INTEGER NOT NULL”

. “, url VARCHAR(80) NOT NULL”

. “, notes VARCHAR(1024)”

. “, counts INTEGER”

. “, time TIMESTAMP DEFAULT sysdate()”

. “)”;

if (mysql_query($sql, $con)) {

print(“Table Tech_links created.\n”);

} else {

print(“Table creation failed.\n”);

}

mysql_close($con);

?>

Remember that mysql_query() returns TRUE/FALSE on CREATE statements. If you run this script, you will get something like this:

Table Tech_links created.

How can we encrypt the username and password using PHP?
Answer1

You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD(“Password”);

Answer2

You can use the MySQL PASSWORD () function to encrypt username and password. For example,

INSERT into user (password …) VALUES (PASSWORD ($password?)) …);

When are you supposed to use endif to end the conditional statement?
When the original if was followed by : and then the code block without braces.
How To Protect Special Characters in Query String?
If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():
What are the differences between DROPS a table and TRUNCATE a table?
DROP TABLE table_name – This will delete the table and its data.

TRUNCATE TABLE table_name – This will delete the data of the table, but not the table definition.

Are objects passed by value or by reference?
Everything is passed by value.
What are the Different Types of Errors in PHP?
Here are three basic types of runtime errors in PHP:

1. Notices: These are trivial, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although you can change this default behavior.

2. Warnings: These are more serious errors – for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

Internally, these variations are represented by twelve different error types

What are “GET” and “POST”?
GET and POST are methods used to send data to the server: With the GET method, the browser appends the data onto the URL. With the Post method, the data is sent as “standard input.”
Give the case where we can use GET and we can use POST methods?
In simple words, in POST method data is sent by standard input (nothing shown in URL when posting while in GET method data is sent through query string.)
How can we submit form without a submit button?
We can use a simple JavaScript code linked to an event trigger of any form field. In the JavaScript code, we can call the document.form.submit() function to submit the form. For example: <input value=”Save”>
How can we create a database using PHP and mysql?
We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.
What is the maximum length of a table name, a database name, or a field name in MySQL?
Database name: 64 characters

Table name: 64 characters

Column name: 64 characters

What are the other commands to know the structure of a table using MySQL commands except EXPLAIN command?
DESCRIBE table_name;
How can we find the number of rows in a table using MySQL?
Use this for MySQL

SELECT COUNT(*) FROM table_name;

What’s the default port for MySQL Server?
3306
How do you change a password for an existing user via mysqladmin?
mysqladmin -u root -p password “newpassword”
Use mysqldump to create a copy of the database?
mysqldump -h mysqlhost -u username -p mydatabasename > dbdump.sql
What are some good ideas regarding user security in MySQL?
There is no user without a password.

There is no user without a user name.

There is no user whose Host column contains % (which here indicates that the user can log in from anywhere in the network or the Internet).

There are as few users as possible (in the ideal case only root) who have unrestricted access.

Explain the difference between FLOAT, DOUBLE and REAL. ?
FLOATs store floating point numbers with 8 place accuracy and take up 4 bytes. DOUBLEs store floating point numbers with 16 place accuracy and take up 8 bytes. REAL is a synonym of FLOAT for now.
What happens if a table has one column defined as TIMESTAMP?
That field gets the current timestamp whenever the row gets altered.