Skip to content
CoursesWeb Forum

What does mysqli_query() expects parameter 1 to be mysqli mean?

Staff answerWritten by Callum Iwu · Error Messages Explained0 replies

Short answer

It means the first value you gave mysqli_query() is not a database connection. Usually the connection variable is empty, false, or a different type, because the connect step failed, the variable is out of scope, or old mysql_query() code was converted with the arguments in the wrong order. Fix the variable that holds the connection, not the query.

Written by Callum Iwu · Moderator: PHP, MySQL, Node.js and AI coding tools ·
CiteCoursesWeb Forum, "What does mysqli_query() expects parameter 1 to be mysqli mean?", https://coursesweb.net/forum/t/what-does-mysqli-query-expects-parameter-1-to-be-mysqli-mean/, accessed 2026-11-17.
  1. Moderator: PHP, MySQL, Node.js and AI coding tools ·

    What the message looks like

    On PHP 7 you see a warning like this:

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /path/page.php on line 12
    

    On PHP 8 and later the same mistake is a fatal error:

    Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given
    

    The word after "given" is the clue. null means the variable never got a value. bool means a false was passed, which is what the procedural mysqli_connect() returns when it fails, according to the PHP manual for mysqli_connect. string means the arguments are swapped.

    A working setup

    Put the connection in one file, and include it where you need it.

    <?php
    // db.php
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    $conn = mysqli_connect('localhost', 'YOUR_DB_USER', 'YOUR_DB_PASSWORD', 'YOUR_DB_NAME');
    mysqli_set_charset($conn, 'utf8mb4');
    
    <?php
    // page.php
    require __DIR__ . '/db.php';
    
    $result = mysqli_query($conn, 'SELECT id, title FROM posts ORDER BY id DESC LIMIT 10');
    
    while ($row = mysqli_fetch_assoc($result)) {
        echo htmlspecialchars($row['title']), "<br>\n";
    }
    

    The first argument is $conn, the connection. The second is the SQL string. The mysqli_query manual page lists the parameters in exactly that order. The mysqli_report line tells PHP to throw an exception when a query or connection fails, so you see the real database error (wrong password, unknown database) instead of a confusing second error later.

    Why it happens

    Three causes cover almost every case.

    1. The connection failed. The variable holds false, or the connect line never ran. The real error is the connect error, and the mysqli_query message is a side effect.
    2. The variable is out of scope. A variable created at the top of a file is not visible inside a function. The function sees an empty $conn.
    3. Old code was half converted. The old mysql_query($sql, $link) took the query first. The new mysqli_query($link, $sql) takes the connection first. The old mysql_ functions were removed in PHP 7, so they cannot be used on a current host.

    The scope fix

    Pass the connection into the function, and ask PHP to check its type:

    <?php
    function latestPosts(mysqli $conn): array
    {
        $result = mysqli_query($conn, 'SELECT id, title FROM posts ORDER BY id DESC LIMIT 10');
        return mysqli_fetch_all($result, MYSQLI_ASSOC);
    }
    
    require __DIR__ . '/db.php';
    $posts = latestPosts($conn);
    

    Avoid global $conn; as a quick patch. It works, but it hides where the connection comes from and breaks the moment you rename it.

    Mistakes beginners make

    • Naming the variable differently in two files. $conn in one and $link or $db in another.
    • Including the connection file after the query. PHP runs top to bottom, so the include must come first.
    • Mixing styles. Use new mysqli() with $mysqli->query(), or use the mysqli_ functions, but be consistent within one file.
    • Putting variables straight into the SQL. Once this error is gone, learn prepared statements (we cover them in a later thread on mysqli and PDO), because the example above is only safe for fixed queries.

    Check this in order

    1. Read the word after "given" in the message: null, bool or string.
    2. Add var_dump($conn); right after the connect line. You should see object(mysqli). If you see bool(false), the connection itself failed.
    3. Turn on mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); before connecting and reload, so the actual database error appears.
    4. Confirm the host, user, password and database name in db.php, and that the include path is right.
    5. Check the argument order in every mysqli_query call: connection first, SQL second.
    6. If the call is inside a function, pass the connection in as an argument.

    The null idea is the same one behind "Cannot read properties of null" in JavaScript, which we explain in Why does getElementById return null and break innerHTML in my script?. The CoursesWeb list of common PHP errors and solutions covers other messages you may meet next.

    1
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.