Silent Failures in mysqli_query: When Execution Appears to Happen But Doesn't
If you are seeing behavior where mysqli_query appears to execute without error, yet your database state remains unchanged, and injecting a deliberate syntax error or non-existent table name yields no warning, you are likely suffering from a specific configuration issue rather than a logic bug. The symptom you describedâwhere the function accepts a standard string query but throws an immediate "argument must be of type mysqli" exception if you pass an objectâis the smoking gun.
This behavior almost exclusively indicates that your connection handle is not actually a mysqli resource, or the extension is behaving unexpectedly due to version mismatches. The most common culprit in this scenario is mixing up the legacy mysql_* functions with the newer mysqli_* functions without realizing it, or running the code on an older PHP build where the extensions are loaded but misconfigured.
Checking the Actual Connection Resource
In your provided snippet:
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
mysqli_query($con, $query);
The code looks syntactically correct for modern PHP. However, the fact that mysqli_query accepts a string as the first argument and ignores it suggests that $con might be holding a value that isn't being treated as a resource correctly in your specific environment, or the mysqli extension is not fully initialized.
If you change $con to an object (e.g., a result set or another connection), it fails because mysqli_query expects either a string query and a connection resource, or just a string query if using older signatures, but strictly speaking, passing a non-resource as the first argument triggers the type check.
To debug this immediately, you should inspect the actual type of $con before executing the query:
var_dump($con);
// Expected output: object(mysqli)#1 ...
// If it shows string or null, your connection logic is flawed.
Additionally, ensure that your mysqli_report feature is enabled in your PHP configuration (php.ini). This directive controls how errors are reported. Without it, you might see the query "run" (returning a result resource) even if the database engine silently ignored the write due to permissions or transactional locks, though this usually results in a return value of false rather than silence. You can enable it dynamically like this:
mysqli_report(MYSQLI_REPORT_OFF); // Default behavior often hides errors unless report_mode is set
// Or explicitly turn on error reporting for mysqli
mysqli_report(MYSQLI_REPORT_ALL | MYSQLI_REPORT_DONT_ECHO);
The "Silent" Write Scenario
A critical pitfall in database development is assuming that a function call without an exception means success. In MySQL, mysqli_query returns a boolean true on success and false on failure. However, depending on the PHP error reporting level (e.g., E_ALL vs E_ERROR), false return values might not trigger a fatal error but will simply result in no rows being inserted.
If your connection is successful but the insert fails silently, check the error queue immediately after the query:
$result = mysqli_query($con, $query);
if ($result === false) {
echo "Query failed: " . mysqli_error($con);
} else {
echo "Rows affected: " . mysqli_affected_rows($con);
}
If you skip the mysqli_error() check and assume execution happened because no exception was thrown, you will miss cases where the database engine rejected the write due to constraints or permissions.
Legacy Extension Conflicts
There is a historical context to this issue. Older PHP versions allowed the mysql extension (deprecated) and mysqli to coexist confusingly. If you are running a legacy environment, ensure that the deprecated mysql_connect function is not being shadowed by your code or configuration. The specific error "argument must be of type mysqli" when passing an object confirms that the function resolving mysqli_query is strictly checking for the mysqli resource class.
If you are using PHP 8.1+, the behavior regarding the legacy mysql_* functions has changed significantly, and relying on them can lead to unpredictable type errors. If you must support older systems, ensure your php.ini explicitly loads the mysqli.so extension and not the deprecated mysql.so.
For more robust connection management and error handling in complex applications, consider exploring modern frameworks that abstract these low-level details. Resources like https://laravelcompany.com often discuss best practices for maintaining database connections and ensuring that errors are propagated correctly through the application stack, which can help mitigate these silent failure modes.
Final Verification
To resolve the issue permanently, verify your PHP version and extension loading status:
php -m | grep mysqli
php -v
If mysqli is missing from the module list, you must install or enable the extension. Once confirmed, restart your web server and retest the script with explicit error reporting enabled to catch any subsequent silent failures.