mysqli_query() expects at least 2 parameters, 1 given

mysqli_query() expects at least 2 parameters, 1 given

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

Stefan Bogdanescu

Founder & Senior Architect

mysqli_query() expects at least 2 parameters, 1 given

The error mysqli_query() expects at least 2 parameters, 1 givenis a straightforward signature violation that occurs when you attempt to execute a raw SQL statement without providing the database connection instance. In PHP's MySQLi extension,mysqli_query` is not a global function that magically knows which database server to talk to; it requires an explicit object representing that connection as its first argument.

The code snippet causing the issue looks like this:

mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)");

Here, you are passing only a string containing the SQL query. The function signature for mysqli_query is defined as mysqli_query(mysqli $link, string|null $query, array|null $params = null): mysqli_result|false. Because the first argument $link is missing, PHP throws a warning immediately before attempting to execute any database logic.

To resolve this, you must instantiate a mysqli object and pass it as the first parameter. The corrected code should look like:

$conn = new mysqli("localhost", "username", "password", "counter");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)";

if (!$conn->query($sql)) {
    echo "Error: " . $conn->error;
} else {
    echo "Record inserted successfully.";
}

$conn->close();

In this example, $conn represents the active connection to the counter database. The logic now follows the expected flow: establish a connection, check for errors, execute the query against that specific connection instance, and finally close the resource.

One of the most common pitfalls when debugging this error is assuming that mysqli_query behaves like mysql_query from the older, deprecated MySQL extension. The legacy mysql_* functions did not require a connection object as an argument because they used global state to track the current connection. If you are migrating code from the old extension or copying snippets from outdated documentation, you might inadvertently leave behind this global dependency assumption. Modern PHP development strictly requires explicit resource management, which is why the connection object must be passed explicitly.

Another critical consideration in your specific example involves the use of prepared statements. While your current query works for inserting a static integer value like 1, using prepared statements is essential when dealing with user input or dynamic values to prevent SQL injection attacks. If you were inserting data provided by a user, passing it directly into the string (even if sanitized) opens you up to vulnerabilities.

Here is how you would handle a dynamic insertion securely:

$pageHits = 1; // Ideally from $_GET or another source, never direct DB input for this column type without validation
$stmt = $conn->prepare("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)");
$stmt->bind_param("i", $pageHits); // 'i' indicates an integer parameter
$stmt->execute();

Using prepare() and bind_param() ensures that the database treats the input strictly as data, not executable code. This is a best practice that separates query structure from data values.

Finally, always ensure you handle connection errors. If the database server is down or credentials are incorrect, the initial new mysqli() call might fail silently in some configurations, leading to confusing downstream errors. Checking $conn->connect_error immediately after instantiation guarantees you have a valid resource before attempting any queries.

If you are building a larger application and need robust database abstraction, consider leveraging Laravel's Eloquent ORM or Query Builder, which handles connection management and prepared statements automatically. You can find more detailed guidance on securing your database interactions at https://laravelcompany.com.

Our network