PHP MySQL INSERT return value with one query execution

PHP MySQL INSERT return value with one query execution

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your INSERT Query Returns Empty Results in Legacy PHP

When executing an INSERT statement in MySQL, the behavior of the result set differs fundamentally from a SELECT query. If you are passing the output of an INSERT into a function expecting a multidimensional array of rows and getting nothing back, it is not a bug; it is the default behavior of the API you are using.

The code snippet provided relies on the mysql_query() function and its associated fetch methods like mysql_fetch_array(). These functions belong to the original MySQL extension for PHP, which was deprecated in PHP 5.5 and removed entirely in PHP 7.0. Crucially, this extension does not support prepared statements or modern binding features, making it unsuitable for secure application development today.

The core issue lies in how mysql_query() handles different SQL statement types. When you execute a SELECT query, the function returns a result resource that represents a cursor over the rows. The while loop and subsequent mysql_fetch_array() calls iterate through this cursor until exhaustion. However, for data-modifying statements like INSERT, UPDATE, or DELETE, MySQL does not return a row set to be iterated over. Instead, the function returns a boolean indicating success or failure.

In your specific implementation:

$results = mysql_query($query, $this->connection);
while($rows = mysql_fetch_array($results, MYSQL_ASSOC)) { ... }

When $query is an INSERT, $results is a boolean (typically true). Passing this boolean to mysql_fetch_array() returns false immediately because the function expects a valid result resource. The loop never executes, leaving $rs as an empty array.

To handle INSERT operations correctly with legacy APIs, you must check the return value of mysql_query() directly rather than attempting to fetch rows. However, relying on mysql_* functions is strongly discouraged due to security vulnerabilities and the lack of support in modern PHP versions. The recommended approach is to migrate to MySQLi or PDO, which handle these distinctions explicitly.

With MySQLi, you can distinguish between query types using mysqli_num_rows() or by checking if a result resource was returned:

$result = $dbh->query($sql);

if ($result === false) {
    // Query failed; check error
} elseif ($result === true) {
    // It's an INSERT/UPDATE/DELETE. 
    // Use get_insert_id() or affected_rows() to get the count, not fetch rows.
    $count = $dbh->affected_rows();
} else {
    // It's a SELECT. Iterate normally.
    while ($row = $result->fetch_assoc()) { ... }
}

PDO offers a similar pattern but with more flexibility:

$stmt = $dbh->prepare("INSERT INTO users (name) VALUES (:name)");
$stmt->execute([':name' => 'John']);

if ($stmt->rowCount() > 0) {
    // Insert succeeded
}

If you are working within a Laravel ecosystem, consider using the Eloquent ORM or the query builder, which abstracts these details away entirely. The DB facade provides a consistent interface where you can check for success without worrying about result resources:

use Illuminate\Support\Facades\DB;

$success = DB::table('users')->insert(['name' => 'Jane']);

if ($success) {
    $id = DB::getPdo()->lastInsertId();
}

The decision to avoid mysql_* functions is not just about version compatibility. The legacy API lacks support for prepared statements, making it impossible to prevent SQL injection attacks effectively. Modern drivers like PDO enforce type checking and parameter binding, ensuring that user input never concatenates directly into the query string. Furthermore, they provide robust error handling mechanisms that allow you to capture specific error codes rather than relying on generic boolean returns or global variables.

If your current codebase still depends on mysql_* functions for legacy reasons, you should prioritize refactoring towards PDO. It is possible to wrap a single PDO connection in a class that mimics the old interface, but this adds unnecessary complexity and maintenance overhead. The trade-off here is simplicity versus security: keeping the old API works for reading data, but it fails completely for writing data without explicit checks, and it exposes your application to significant risk.

Ultimately, the empty array you are seeing is a signal that the query type does not produce a result set. By switching to a modern driver and checking the appropriate return values—such as affected_rows() for writes or iterating the result resource for reads—you can build a more secure and maintainable application.

Our network