PHP PHP Database Integration & MySQL 1 — Questions and Answers
Question 1: What does PDO stand for in PHP?
- PHP Data Output
- PHP Data Objects (Correct answer)
- Persistent Database Operations
- PHP Driver Object
Correct answer: PHP Data Objects
PDO stands for PHP Data Objects, a database abstraction layer providing a consistent interface for multiple database types.
Question 2: Why are prepared statements preferred over plain SQL queries in PHP?
- They execute faster than all other methods
- They automatically index database tables
- They prevent SQL injection attacks (Correct answer)
- They require no database connection
Correct answer: They prevent SQL injection attacks
Prepared statements separate SQL logic from user data, ensuring malicious input cannot alter the query structure.
Question 3: Which PDO method prepares an SQL statement for execution?
- execute()
- query()
- prepare() (Correct answer)
- compile()
Correct answer: prepare()
PDO::prepare() sends the SQL template to the database and returns a PDOStatement object ready for binding and execution.
Question 4: Which PDO method retrieves all rows of a result set as an array?
- getAll()
- fetchAll() (Correct answer)
- selectAll()
- resultAll()
Correct answer: fetchAll()
fetchAll() fetches all remaining rows from the result set and returns them as a multidimensional array.
Question 5: Which PDO constant returns each row as an associative array keyed by column name?
- PDO::FETCH_NUM
- PDO::FETCH_OBJ
- PDO::FETCH_BOTH
- PDO::FETCH_ASSOC (Correct answer)
Correct answer: PDO::FETCH_ASSOC
PDO::FETCH_ASSOC returns each row as an associative array where keys are the column names from the result.
Question 6: What does the PDO lastInsertId() method return after an INSERT statement?
- The number of rows inserted
- The SQL query that was run
- The ID of the last inserted row (Correct answer)
- The name of the table
Correct answer: The ID of the last inserted row
lastInsertId() returns the auto-incremented primary key value assigned to the most recently inserted row.
What does PDO stand for in PHP?