mysqli_query() refuses the connection you pass it
Warning: mysqli_query() expects parameter 1 to be mysqli, resource givenShort answer
mysqli_query() needs the mysqli connection as its first argument and the SQL string second. The error means the first argument is something else: the arguments are swapped, the connection variable is not visible inside the function, or it comes from the removed mysql_connect(). Pass a real mysqli object first.
Fix it fast
1Find out which cause you have
Print what the first argument really is, right before the failing call.
var_dump($conn); // object(mysqli)#1 is good; NULL, string or resource is not2Match what you see, then jump to its fix
- 1
The connection was made with mysql_connect(), and the message says resource given.The mysql_ and mysqli_ functions were mixedFix - 2
The call looks like mysqli_query($sql, $conn).The arguments are in the wrong orderFix - 3
The same query works at the top of the file and fails inside a function.The connection variable is not visible inside the functionFix - 4
mysqli_connect() returned false, then the next line used it.The connection failed and the code kept goingFix
- 1
What does the error look like?
Warning: mysqli_query() expects parameter 1 to be mysqli, resource givenmysqli_query(): Argument #1 ($mysql) must be of type mysqli
Cause 1 of 4
The mysql_ and mysqli_ functions were mixed
1Check
The old mysql extension was removed in PHP 7.0. A connection from it is a resource, never a mysqli object. Search for any mysql_ function left in the code.
grep -rn "mysql_connect\|mysql_query\|mysql_select_db" .2Fix
Replace every mysql_ call with mysqli (or PDO) and connect with mysqli_connect() or new mysqli().
$conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($conn, "SELECT 1");3Verify
Run the page. mysqli_query() now returns a mysqli_result.
php -l db.php && php db.phpConfirmed by php.net
Cause 2 of 4
The arguments are in the wrong order
1Check
In procedural style the connection comes first and the query second. Many old examples had them the other way round.
grep -rn "mysqli_query(" .2Fix
Swap them. The object style avoids the question entirely, because the connection is the object.
// procedural
$result = mysqli_query($conn, $sql);
// object style, same thing
$result = $conn->query($sql);3Verify
Run the page again.
php db.phpConfirmed by php.net
Cause 3 of 4
The connection variable is not visible inside the function
1Check
Variables created outside a function do not exist inside it. The function sees an undefined local variable, so the first argument is NULL.
function getUsers() {
var_dump($conn); // NULL here
}2Fix
Pass the connection in as a parameter. global and $GLOBALS also work but hide the dependency.
function getUsers(mysqli $conn): array {
$result = mysqli_query($conn, "SELECT id FROM users");
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
$users = getUsers($conn);3Verify
Call the function and print the count.
var_dump(count(getUsers($conn)));Confirmed by php.net
Cause 4 of 4
The connection failed and the code kept going
1Check
mysqli_connect() returns false on failure, while new mysqli() still returns an object even if the connection failed, so you must check connect_error yourself.
$conn = mysqli_connect("localhost", "my_user", "my_password", "my_db");
var_dump($conn, mysqli_connect_error());2Fix
Turn on mysqli exceptions before connecting, so a bad connection stops with a clear message instead of a later type error.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli("localhost", "my_user", "my_password", "my_db");3Verify
Use a wrong password on purpose; the script should stop with the connection error.
php db.phpConfirmed by php.net
How do I stop it coming back?
- Create the connection once and pass it to functions and classes, never rely on a global.
- Declare parameters as mysqli $conn so a wrong value fails at the function boundary.
- Use prepared statements for any value that comes from a user.
- Remove old mysql_ code during upgrades; PHP no longer ships it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
mysqli_fetch_array() says the argument must be a mysqli_result, but got something else
mysqli_sql_exception (PHP 8.1 and later, when the query itself failed)
mysqli_fetch_array() only accepts a mysqli_result object.
3 causes, each with a fixBack end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fix