Sharpen your understanding of basic PHP and MySQL CRUD operations with this quiz, featuring questions on creating, reading, updating, and deleting records. Improve your grasp of SQL queries, PHP integration, and essential database concepts for web development.
Which SQL statement correctly inserts a new row into a table named 'users' with columns 'name' and 'email'?
Explanation: The INSERT INTO statement is used to add new rows to a table. UPDATE modifies existing rows, not adding new ones, while DELETE is used for removal, and SELECT retrieves data. Option A properly uses the INSERT syntax and provides both column names and values.
In PHP and MySQL, what is the main purpose of issuing a SELECT statement like 'SELECT * FROM posts'?
Explanation: SELECT is used to fetch data from a database table. The asterisk * means all columns will be retrieved. DELETE removes data, INSERT adds new data, and UPDATE modifies current entries, so the other options do not match the SELECT statement's purpose.
Which SQL statement properly updates the email of a user with id 1 in the 'users' table?
Explanation: UPDATE changes existing data, and a WHERE clause specifies which row to change. INSERT adds a row, DELETE removes data, and SELECT is for viewing. Only option A correctly updates the specified user's email.
What does the SQL statement 'DELETE FROM comments WHERE id=5;' do?
Explanation: DELETE FROM specifies a row for removal by the WHERE clause. INSERT is used for adding data, UPDATE for changing values, and SELECT for fetching data, making the other options incorrect.
Which PHP function is commonly used to establish a connection to a MySQL database?
Explanation: mysqli_connect() is the standard function for connecting PHP to a MySQL database. mysql_insert() and connect_sql() are not valid PHP built-in functions, and mysqli_query() only executes SQL queries after a connection is made.
What method should you use in PHP to safely include user input in SQL queries to prevent SQL injection?
Explanation: Prepared statements separate SQL logic from data, preventing injections. Concatenating user input exposes you to risks, echo does not affect SQL safety, and letter casing is unrelated. Only parameter binding ensures queries are executed safely.
After running a SELECT query in PHP, which function retrieves a row as an associative array?
Explanation: mysqli_fetch_assoc() fetches a row as an associative array mapping columns to values. mysqli_exec() does not exist, mysqli_num_rows() counts rows, and mysqli_insert_id() gets the ID from the last insert, not for fetching result rows.
Why should every table in a relational database ideally have a primary key column?
Explanation: The primary key ensures each row has a unique identifier, which is essential for managing data. It does not store backups or directly define foreign keys (but can be referenced), and it is not meant for storage expansion.
Which PHP function is typically used to send an SQL statement to a MySQL database for execution after a connection is established?
Explanation: mysqli_query() is the correct function for executing SQL queries after connecting to MySQL. connect_query() and sql_send() are not standard PHP functions, and mysqli_fetch_query() does not exist in default PHP extensions.
How can you check for errors after attempting an SQL operation using PHP’s MySQLi extension?
Explanation: mysqli_error() returns a description of the most recent error involving the database connection. $_POST is related to form data, new connections don't indicate errors, and UPDATE only changes data without checking for errors by itself.