“mysqli::query(): Argument #1 ($query) must not be empty” means the application passed an empty string instead of SQL. Validate the statement where it is assembled and fail with a specific application error before calling MySQLi.
Last updated: October 31, 2026.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli($host, $user, $password, $database);
$sql = trim((string) ($generatedSql ?? ''));
if ($sql === '') {
throw new LogicException('No SQL statement was generated.');
}
$stmt = $db->prepare($sql);
$stmt->execute();The guard identifies the real application failure. It does not invent a fallback query or send an empty statement to the database. Keep the validation immediately beside the query-building boundary so later code can rely on a non-empty statement.
Trace why the statement is empty
The PHP manual for mysqli::query() requires a SQL string. An empty value commonly comes from an unrecognized route, a missing template key, a failed query builder branch, or a URL parameter that was expected to carry an encoded query.
Log the code path and a safe statement identifier, not passwords, tokens, or the full request. Check each branch that assigns the SQL variable and add a test for the input that produced the empty value.
Keep data out of the SQL string
Do not fix the error by concatenating request values into a default query. The MySQLi prepared-statement API keeps SQL structure separate from runtime values. Build a known statement, validate it, prepare it, and bind values with their expected types.
If an operation is optional, skip the database call explicitly instead of using an empty query as a control signal. Add unit tests for every supported query-builder branch, including missing filters and unknown operation names. Continue with reading database exception details, connecting PHP to MySQL, and handling PHP throwable errors.