MYSQL Interview Questions

There is given MYSQL interview questions and answers that have been asked in many companies. Let's see the list of top MYSQL interview questions.

What is autoloading classes in PHP?

With autoloaders, PHP allows the last chance to load the class or interface before it fails with an error.

The spl_autoload_register() function in PHP can register any number of autoloaders, enable classes and interfaces to autoload even if they are undefined.

spl_autoload_register(function($classname){include $classname.'.php';
});
$object = new Class1();
$object2 = new Class2();

In the above example, we do not need to include Class1.php and Class2.php. The spl_autoload_register() function will automatically load Class1.php and Class2.php.

.

Could you explain how to fetch data from a MySQL database using PHP?

To begin with, you need to first establish a connection with the MySQL database that you wish to use. For that, you can use the mysqli_connect() function. Suppose that the database that you need to access is stored on a server named localhost and has the name instanceDB. Also, it has user_name as the username and pass_word as the password. For establishing a connection to the instanceDB, you need to use the following PHP code:

<?php
$servername = “localhost”;
$username = “user_name”;
$password = “pass_word”;
$dbname = “instanceDB”;
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) { // For checking connection to the database
 die(“Connection failed: ” . mysqli_connect_error());
}
Next, you need to use the SELECT statement to fetch data from one or more tables. The general syntax is:
SELECT column_name from table_name
Suppose, we have a single table called instancetable with column_1, column_2, and column_3 in the instanceDB, then to fetch data; we need to add the following PHP code:
$sql = “SELECT column_1, column_2, column_3 from instancetable”;
$result = $conn->query($sql);
.

How to create a database connection and query in PHP?

To create a database connection:

$connection = new mysqli($servername, $username, $password);
where $servername, $username, $password should be defined beforehand by the developer.
To check if the connection was successful:
if ($conn->connect_error) {
 die("Connection error: " . $conn->connect_error);
}
Create database query:
$sql = "CREATE DATABASE PRODUCT";
if ($conn->query($sql) === TRUE) {
 echo "Database successfully created";
} else {
 echo "Error while creating database: " . $conn->error;
}
.

How we can retrieve the data in the result set of MySQL using PHP?

mysql_fetch_row mysql_fetch_array mysql_fetch_object mysql_fetch_assoc.

How to execute an sql query? How to fetch its result ?

$my_qry = mysql_query(“SELECT * FROM `users` WHERE `u_id`=’1′; “); $result = mysql_fetch_array($my_qry); echo $result[‘First_name’];.

How to select a MySQLdatabase?

mysql_select_db($db_name);
.

What are all the Common MySQL Function?

CONCAT(A, B) – Concatenates two string values to create a single string output. Often used to combine two or more fields into one single field. FORMAT(X, D) – Formats the number X to D significant digits. CURRDATE(), CURRTIME() – Returns the current date or time. NOW() – Returns the current date and time as one value. MONTH(), DAY(), YEAR(), WEEK(), WEEKDAY() – Extracts the given data from a date value. HOUR(), MINUTE(), SECOND() – Extracts the given data from a time value. DATEDIFF(A, B) – Determines the difference between two dates and it is commonly used to calculate age SUBTIMES(A, B) – Determines the difference between two times. FROMDAYS(INT) – Converts an integer number of days into a date value..

What are the objects can be created using CREATE statement in MySQL?

Following objects are created using CREATE statement:

  • DATABASE
  • EVENT
  • FUNCTION
  • INDEX
  • PROCEDURE
  • TABLE
  • TRIGGER
  • USER
  • VIEW
.

How does DISTINCT work in MySQL?

DISTINCT is used to avoid the problem of duplicity while fetching the results of a particular query. DISTINCT is used to make sure the results do not contain repeated values. DISTINCT can be used with the SELECT clause. Here is the syntax for it:

SELECT DISTINCT something FROM tablename;
.

How to add USERS in MySQL?

To simply put, the user can be added by using the CREATE command and specifying the necessary credentials. First, log in to the MySQL account and then apply the syntax. Something like this:

CREATE USER ‘testuser’ IDENTIFIED BY ‘sample password’;
Users can be granted permissions, by the following commands:
GRANT SELECT ON * . * TO ‘testuser’;
.