In PHP using the mysqli extension, which function is typically used to execute an INSERT statement to add a new record to a MySQL database table?

Difficulty: Easy

Correct Answer: mysqli_query()

Explanation:


Introduction / Context:
When building PHP applications that interact with MySQL, you frequently need to insert new records into database tables. Using the mysqli extension, PHP provides a set of functions for connecting to the database, executing queries, retrieving results, and getting metadata. Understanding which function actually sends an INSERT statement to the database is fundamental for performing create operations in a typical CRUD (Create, Read, Update, Delete) workflow.


Given Data / Assumptions:

  • We are using the procedural style of the PHP mysqli extension.
  • We have already established a successful connection to a MySQL database using mysqli_connect().
  • We want to execute an INSERT SQL statement, such as INSERT INTO users(name, email) VALUES(...).
  • We must choose the correct mysqli function to send this SQL statement to the server.


Concept / Approach:
In mysqli, the function responsible for sending an SQL query to the database server is mysqli_query(). It can execute any valid SQL statement, including SELECT, INSERT, UPDATE, and DELETE. For an INSERT operation, mysqli_query() returns true on success or false on failure in procedural style. Other related functions, such as mysqli_insert_id(), do not execute queries themselves but instead retrieve information (like the last auto increment value) after a successful INSERT has been performed.


Step-by-Step Solution:
Step 1: Use mysqli_connect() to establish a connection to the MySQL server and obtain a connection resource or object. Step 2: Prepare an INSERT SQL string, for example $sql = 'INSERT INTO users(name, email) VALUES("John", "john@example.com")'. Step 3: Call mysqli_query($conn, $sql) to send the INSERT statement to the database via the established connection. Step 4: Check the return value of mysqli_query(); if it is true, the INSERT succeeded; if false, there was an error. Step 5: Optionally call mysqli_insert_id($conn) afterwards to obtain the last auto incremented ID if the table uses an AUTO_INCREMENT primary key.


Verification / Alternative check:
The PHP manual for mysqli_query() clearly states that the function performs a query against the database. Example code demonstrates using mysqli_query() for INSERT, UPDATE, DELETE, and SELECT statements. Testing a sample script confirms that without calling mysqli_query(), no new record appears in the database, while calling mysqli_query() with a valid INSERT SQL string creates the row as expected.


Why Other Options Are Wrong:
Option B (mysqli_connect()) is used to establish a connection, not to execute SQL statements. Option C (mysqli_fetch_assoc()) retrieves rows from a result set produced by a SELECT query and cannot execute an INSERT. Option D (mysqli_insert_id()) only returns the ID generated by a previous INSERT, but it does not perform the insertion itself.


Common Pitfalls:
A common pitfall is failing to check the return value of mysqli_query() and not handling errors, which makes debugging difficult. Another mistake is concatenating unsanitized user input directly into the SQL string, which can expose the application to SQL injection attacks. In production code, developers should combine mysqli_query() with prepared statements (mysqli_prepare, mysqli_stmt_bind_param) or use PDO with parameterized queries to execute INSERTs safely.


Final Answer:
The mysqli function typically used to execute an INSERT statement and add a new record is mysqli_query().

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion