MCQs – PHP and MySQL Integration
Which PHP extensions can be used to connect to a MySQL database? a) MySQLi (object-oriented) b) MySQLi (procedural) c) PDO (PHP Data Objects) d) All of the above ✅ Answer: d) All of the above
Which PHP extension supports multiple database systems besides MySQL? a) MySQLi b) PDO c) mysql_connect d) mysqli_connect ✅ Answer: b) PDO
In PHP, what function is used to terminate the script and output a message upon a connection failure? a) exit() b) stop() c) die() d) terminate() ✅ Answer: c) die()
What does the mysqli::connect_error
property return?
a) The last executed SQL query
b) The error message from the last connection attempt
c) The number of affected rows
d) The current database name
✅ Answer: b) The error message from the last connection attempt
Which function is used to perform a query on the database using MySQLi? a) mysqli_execute() b) mysqli_query() c) mysqli_run() d) mysqli_fetch() ✅ Answer: b) mysqli_query()
What does the mysqli::close
function do?
a) Closes the PHP script
b) Ends the MySQL server process
c) Closes a previously opened database connection
d) Deletes the database
✅ Answer: c) Closes a previously opened database connection
Which of the following is a correct way to create a new MySQLi connection in PHP?
a) new mysqli("localhost", "user", "password");
b) mysqli_connect("localhost", "user", "password");
c) Both a and b
d) None of the above
✅ Answer: c) Both a and b
What is the default port number for MySQL server connections? a) 3306 b) 8080 c) 1433 d) 1521 ✅ Answer: a) 3306
Which function returns the last error message for the most recent MySQLi function call? a) mysqli::error b) mysqli::get_error c) mysqli::last_error d) mysqli::error_message ✅ Answer: a) mysqli::error
Which PHP function is used to fetch a result row as an associative array? a) mysqli_fetch_row() b) mysqli_fetch_array() c) mysqli_fetch_assoc() d) mysqli_fetch_object() ✅ Answer: c) mysqli_fetch_assoc()
Here are multiple-choice questions (MCQs) based on the content you provided from the “MySQL with PHP: Database Connection Guide & Examples”:
1. Which PHP extension supports both object-oriented and procedural styles for MySQL interaction? A. PDO B. MySQL C. MySQLi D. SQLite ✅ Answer: C. MySQLi
2. Which of the following is a correct function to connect to a MySQL database using MySQLi (object-oriented)?
A. mysql_connect()
B. mysqli_connect()
C. new mysqli()
D. pdo_connect()
✅ Answer: C. new mysqli()
3. What does the die()
function do in PHP?
A. Closes database connection
B. Terminates script execution with an optional message
C. Starts a session
D. Hides PHP errors
✅ Answer: B. Terminates script execution with an optional message
4. Which method is used to execute SQL queries using a MySQLi object?
A. run()
B. query()
C. execute()
D. fire()
✅ Answer: B. query()
5. What does $conn->connect_error
return?
A. Error code
B. Last executed query
C. Last connection error message
D. Number of failed queries
✅ Answer: C. Last connection error message
6. What is the main difference between include
and require
in PHP?
A. include
is for HTML, require
is for PHP
B. include
gives a fatal error on failure
C. require
halts script on failure, include
does not
D. No difference
✅ Answer: C. require
halts script on failure, include
does not
7. What does ===
do in PHP that ==
does not?
A. Compares string length
B. Checks both value and data type
C. Compares two arrays
D. Compares the memory address
✅ Answer: B. Checks both value and data type
8. Which SQL statement is used to create a new table in MySQL?
A. MAKE TABLE
B. ADD TABLE
C. CREATE TABLE
D. NEW TABLE
✅ Answer: C. CREATE TABLE
9. Which PHP function is used to execute multiple queries at once?
A. mysqli::query()
B. mysqli::multi_query()
C. mysqli::run()
D. mysqli::execute_many()
✅ Answer: B. mysqli::multi_query()
10. What does fetch_assoc()
return?
A. Indexed array
B. JSON string
C. Associative array
D. Number of rows
✅ Answer: C. Associative array