PHP Questions & Answers – Working with Databases-2

This set of PHP Multiple Choice Questions & Answers (MCQs) focuses on “Working with Databases-2”.

1. Which one of the following statements should be used to include a file?
a) #include ‘filename’;
b) include ‘filename’;
c) @include ‘filename’;
d) #include <filename>;
View Answer

Answer: b
Explanation: Include in PHP will takes all the code from the specified file and copies to the existing file containing include statement. An example of this-

  1. <?php
  2. #include 'mysql.connect.php';
  3. //begin database selection and queries.
  4. ?>

2. Which one of the following methods is responsible for sending the query to the database?
a) query()
b) send_query()
c) sendquery()
d) mysqli_query()
View Answer

Answer: d
Explanation: The method mysqli_query() is responsible for sending the query to the database. Query() method was previously used in older versions of PHP.
advertisement
advertisement

3. Which one of the following methods recuperates any memory consumed by a result set?
a) destroy()
b) mysqli_free_result()
c) alloc()
d) free()
View Answer

Answer: b
Explanation: The function mysqli_free_result() is used to free the memory which is associated with the result. Once this method is executed, the result set is no longer available. Free() function was used in the previous version of PHP.

4. Which of the methods are used to manage result sets using both associative and indexed arrays?
a) get_array() and get_row()
b) get_array() and get_column()
c) fetch_array() and fetch_row()
d) mysqli_fetch_array() and mysqli_fetch_row()
View Answer

Answer: d
Explanation: The method mysqli_fetch_array() is used to fetch a result row as an associative array or a numeric array.
And the function mysqli_fetch_row() is used to fetche one row from a result-set and returns it as an enumerated array.
The method fetch_array() and fetch_row() were used in the previous version of PHP.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. Which one of the following method is used to retrieve the number of rows affected by an INSERT, UPDATE, or DELETE query?
a) num_rows()
b) affected_rows()
c) changed_rows()
d) mysqli_affected_rows()
View Answer

Answer: d
Explanation: The method mysqli_num_rows() is only useful for determining the number of rows retrieved by a SELECT query. But to retrieve the number of rows affected by INSERT, UPDATE, or DELETE query, use mysqli_affected_rows(). Num_rows() and affected_rows() were used in previous version of PHP.

6. Which version of MySQL introduced the prepared statements?
a) MySQL 4.0
b) MySQL 4.1
c) MySQL 4.2
d) MySQL 4.3
View Answer

Answer: b
Explanation: When the query() method is looped repeatedly it comes at a cost of both overhead, because of the need to repeatedly parsing of the almost identical query for validity, and coding convenience, because of the need to repeatedly reconfigure the query using the new values for each iteration. To help resolve the issues incurred by repeatedly executed queries, MySQL introduced prepared statements.
advertisement

7. Which of the following methods is used to execute the statement after the parameters have been bound?
a) bind_param()
b) bind_result()
c) bound_param()
d) bound_result()
View Answer

Answer: a
Explanation: Once the statement has been prepared, it needs to be executed. Exactly when it’s executed depends upon whether you want to work with bound parameters or bound results. In the case of bound parameters, you’d execute the statement after the parameters have been bound with the bind_param() method.

8. Which one of the following methods is used to recuperating prepared statements resources?
a) end()
b) finish()
c) mysqli_close()
d) close()
View Answer

Answer: c
Explanation: The function mysqli_close() is used to close an opened database connection. Once you’ve finished using a prepared statement, the resources it requires can be recuperated with the mysqli_close() method. Close() was used in previous version of PHP.
advertisement

9. Which method retrieves each row from the prepared statement result and assigns the fields to the bound results?
a) get_row()
b) fetch_row()
c) fetch()
d) mysqli_fetch_row()
View Answer

Answer: d
Explanation: The function mysqli_fetch_row() is used to fetche row from a result-set and returns it as an enumerated array.
Its syntax is mysqli_fetch_row(result);

10. Which method rolls back the present transaction?
a) commit()
b) undo()
c) mysqli_rollback()
d) rollback()
View Answer

Answer: c
Explanation: The function mysqli_rollback() is used to roll back from the current transaction for the specified database connection. Its syntax is: mysqli_rollback(connection);
Rollback() was used in previous version of PHP.

Sanfoundry Global Education & Learning Series – PHP Programming.

To practice all areas of PHP, here is complete set of 1000+ Multiple Choice Questions and Answers on PHP.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.