How to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP?

Spread the love

Sometimes, we want to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP.

In this article, we’ll look at how to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP.

How to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP?

To fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP, we can use prepared statements.

For instance, we write

$stmt = $mysqli->prepare(
    "INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)"
);
$stmt->bind_param("s", $hits);
$stmt->execute();

to call prepare with the SQL query string to make a prepared statement object.

Then we call bind_params to bind the $hits value to the ? placeholder in the SQL string.

And then we call execute to run the prepared statement.

Conclusion

To fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP, we can use prepared statements.

Leave a Reply

Your email address will not be published.