What does mysqli_query() expects parameter 1 to be mysqli mean?
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.
Cite
CoursesWeb 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.- Callum IwuStaffModerator: 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 12On 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 givenThe word after "given" is the clue.
nullmeans the variable never got a value.boolmeans afalsewas passed, which is what the proceduralmysqli_connect()returns when it fails, according to the PHP manual for mysqli_connect.stringmeans 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. Themysqli_reportline 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.
- The connection failed. The variable holds
false, or the connect line never ran. The real error is the connect error, and themysqli_querymessage is a side effect. - 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. - Old code was half converted. The old
mysql_query($sql, $link)took the query first. The newmysqli_query($link, $sql)takes the connection first. The oldmysql_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.
$connin one and$linkor$dbin 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 themysqli_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
- Read the word after "given" in the message: null, bool or string.
- Add
var_dump($conn);right after the connect line. You should seeobject(mysqli). If you seebool(false), the connection itself failed. - Turn on
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);before connecting and reload, so the actual database error appears. - Confirm the host, user, password and database name in db.php, and that the include path is right.
- Check the argument order in every
mysqli_querycall: connection first, SQL second. - 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.
- The connection failed. The variable holds
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.