Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

mysqli_query() refuses the connection you pass it

ErrorWarning: mysqli_query() expects parameter 1 to be mysqli, resource given

Short 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.

  • Applies to PHP 8.3, PHP 8.4, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 not
  2. 2Match what you see, then jump to its fix

    1. 1The connection was made with mysql_connect(), and the message says resource given.The mysql_ and mysqli_ functions were mixedFix
    2. 2The call looks like mysqli_query($sql, $conn).The arguments are in the wrong orderFix
    3. 3The same query works at the top of the file and fails inside a function.The connection variable is not visible inside the functionFix
    4. 4mysqli_connect() returned false, then the next line used it.The connection failed and the code kept goingFix

What does the error look like?

  • Warning: mysqli_query() expects parameter 1 to be mysqli, resource given
  • mysqli_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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.