Parameter Order: The Root Cause of mysqli_query() Warnings
PHP's procedural mysqli extension has a strict signature requirement that often trips up developers migrating from MySQLi to PDO or those new to the API. The error message Warning: mysqli_query() expects parameter 1 to be mysqli, string given is not a cryptic riddle; it is a direct indicator of incorrect function argument ordering.
In your specific case, the issue stems from passing the SQL query string as the first argument and the connection resource as the second. The standard procedural mysqli_query() function expects the MySQLi connection object (or handle) as its first parameter, followed by the SQL statement. When you provide them in reverse, PHP interprets the string $sql as the connection object because it is in the first position. Since a string does not possess the internal methods required for a database connection, the function fails immediately upon execution.
To resolve this, you must swap the arguments in your function call. The corrected implementation looks like this:
<?php
require_once 'conn.php';
$sql = "SELECT user_id, access_lvl, name FROM cms_users";
$result = mysqli_query($conn, $sql);
// Always check the result for errors or false values before proceeding
if ($result) {
// Process results here
} else {
echo mysqli_error($conn);
}
Understanding the Procedural vs. Object-Oriented Distinction
While your current code relies on the procedural interface, it is worth noting that mysqli_query() behaves differently depending on which API layer you are using. In the object-oriented approach, which is often preferred for modern PHP applications due to better encapsulation and method chaining, you would instantiate a mysqli object directly within the class or file:
<?php
$connection = new mysqli($host, $username, $password, $dbname);
// The query method here takes only the SQL string as the argument
// because `$this` (the connection) is implicit.
$result = $connection->query("SELECT user_id, access_lvl, name FROM cms_users");
if ($connection->error) {
die($connection->error);
}
This distinction highlights why the warning occurred in your procedural code but not necessarily in an OOP context. In the procedural API, the connection handle is explicit and must be passed as an argument; in the object-oriented API, the context is self-evident to the method call. Mixing these paradigms without understanding the underlying signature requirements leads directly to the type mismatch error you encountered.
Security Considerations with Dynamic Queries
Your current query string uses a static SQL statement: SELECT user_id, access_lvl, name FROM cms_users. Since no variables are being interpolated directly into the string, this specific snippet is not vulnerable to SQL injection at this moment. However, if your $sql variable were constructed using user input or database values (e.g., filtering by a specific user ID passed via POST), you would need to switch immediately to prepared statements.
Using mysqli_prepare() alongside mysqli_stmt_execute() is the robust alternative for dynamic queries. It separates the SQL logic from the data, ensuring that user inputs are treated strictly as data and never as executable code. This practice prevents injection attacks regardless of whether you are using procedural or object-oriented syntax.
Best Practices for Connection Management
The line require_once 'conn.php'; suggests that your database credentials and connection logic reside in an external file. While this is a good separation of concerns, ensure that conn.php initializes the connection correctly and returns a valid resource. If conn.php uses the object-oriented interface internally but you are trying to pass it to the procedural function, you must verify the type returned.
// Ensure conn.php returns a compatible mysqli object or handle
// For procedural usage, ensure the variable passed is indeed the connection handle
$conn = require_once 'conn.php';
// Or if conn.php just sets $conn globally:
global $conn;
If conn.php initializes the connection using new mysqli(), it returns an object. The procedural function can accept this object, but strictly speaking, many tutorials recommend explicitly casting or ensuring the variable type matches expectations to avoid ambiguity in complex codebases.
For production environments, consider leveraging a framework like Laravel. The LaravelCompany platform offers robust tools for managing database connections using Eloquent ORM or raw queries with proper parameter binding. Their architecture handles connection pooling and query execution safety automatically, reducing the risk of argument order errors or injection vulnerabilities inherent in raw procedural calls.