PHP-MySQL

Most Essential And Frequently Asked Interview Questions And Answer

Q:What are the difference between mysqli_fetch_assoc and mysqli_fetch_array?

Ans:

PHP-MYSQL |  mysqli_fetch_assoc() VS  mysqli_fetch_array()

mysqli_fetch_assoc() function fetch a result row as an associative array where as mysqli_fetch_array() function fetch a result row as an associative array, a numeric array, or both.

View Detials

Q:What are the difference between mysqli_fetch_object and mysqli_fetch_array?

Ans:

PHP-MYSQL |  mysqli_fetch_object () VS  mysqli_fetch_array()

mysqli_fetch_object()  fetch a result row as an object where as mysqli_fetch_array()  fetch a result row as an associative array, a numeric array, or both.

View Detials

Q:How can we optimize a table?

Ans:

OPTIMIZE command is use for optimize a table.

 OPTIMIZE TABLE `users` 

 

Q:how many ways we can retrieve the data in the result set of MySQL using PHP?

Ans:

PHP-MYSQL |  Fetch Recoards From MySql table

There are 4 ways:

  1. mysqli_fetch_row() - Get a result row as an enumerated array
  2. mysqli_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
  3. mysqli_fetch_object() - Fetch a result row as an object
  4. mysqli_fetch_assoc() - Fetch a result row as an associative array
View Detials

Q:How can we increase the speed of a MySQL select query?

Ans:

For increaseing the speed of a query we have to do couple of things

  • First of all instead of using select * from table1, use select column1, column2, column3.. from table1.
  • Try to introduce index in the table.
  • Always use limit keyword if we are looking for any specific number of rows from the result set.

Q:How can we know the number of days between two given dates using MySQL?

Ans:

DATEDIFF function is use for finding the number of days between two given dates using MySQL

 DATEDIFF(‘2018-03-07′,’2017-01-01’)

 

Q:How can we find the number of rows in a result set using PHP?

Ans:

Bellow code is use for the same

 $sql = "SELECT * FROM table1";
$result = mysql_query($sql, $db_link);
$num_rows = mysql_num_rows($result);
echo $num_rows;

 

Q:What are the different storage engine present in MySQL?

Ans:

There are 5 different types of storage engine present in MySql.

  1. MyISAM
  2. Heap
  3. Merge
  4. INNO DB
  5. ISAM

MyISAM is the default storage engine for MySQL

Q:what is Cross-site scripting?

Ans:

Cross-site scripting (XSS)

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side script into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.

Q:What is the best collation to use for MySQL?

Ans:

Mostly we use:

  • utf8_general_ci
  • utf8_unicode_ci

utf8_general_ci is use for fast sorting but it is less accurate whereas utf8_unicode_ci is used for better accuracy but little slow as compare to utf8_general_ci.

We can also use other collation based on specific languages like utf8_swedish_ci.

Q:What are the 3 different ways in PHP to connect with MySQL?

Ans:

The three different way in PHP to connect with MySQL is:

  1. MySQL
  2. MySQLI
  3. PDO

Note: We should use MySQLI because MySQLI is imporoved version of MySQL and have more function then MySQL.

Q:How we can find last insert id in table?

Ans:

By using mysql function mysqli_insert_id() we can retrive the last inserted id in database table.

Today spacial posts