Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_query() expects at least 2 parameters, 1 given

Tags:

php

mysqli

This mysqli_query command results in the error below

mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 

Warning: mysqli_query() expects at least 2 parameters, 1 given in

What does this error message mean, and how can it be fixed?

like image 330
Joe Winfield Avatar asked Nov 09 '11 23:11

Joe Winfield


People also ask

How do you solve mysqli_query () expects at least 2 parameters 1 given?

You need to provide the handle to the database as the first argument then the query as t second argument, e.g. $db = mysqli_connect('localhost', 'myUserName', 'myPassword',, 'myDatabaseName'); $query = 'SELECT id FROM myTable'; $result = mysqli_query($db, $query);

How many parameter mysqli_query () Atleast it takes?

Error: mysqli_query() expects at least 2 parameters, 1 given.

What does the mysqli_query () function do?

Definition and Usage. The mysqli_query() function accepts a string value representing a query as one of the parameters and, executes/performs the given query on the database.


2 Answers

mysqli_query excepts 2 parameters, the first variable is mysqli_connect equivalent variable, the second one is the query you have provided

$name1 = mysqli_connect(localhost,db_username ,db_pswd ,db_name );

$name2 = mysqli_query($name1,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)");
like image 177
Shanaka WickramaArachchi Avatar answered Oct 01 '22 15:10

Shanaka WickramaArachchi


If you get any of the following errors:

mysqli_query() expects at least 2 parameters, 1 given

mysqli_select_db() expects exactly 2 parameters

mysqli_real_escape_string() expects exactly 2 parameters

It means that you have not passed the mandatory parameter to these functions. MySQLi procedural style functions expect the first parameter to be a valid MySQLi connection link.

For example, mysqli_query expects a database link as the first argument and the actual SQL query as a second argument.

Assuming you have this or similar connection code somewhere at the start of you script.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass, $db);
mysqli_set_charset ($mysqli, 'utf8mb4');

You can use the connection link saved in $mysqli and pass it as a first argument.

mysqli_query($mysqli, "INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 

Most of the mysqli procedural functions require the connection to be passed as an argument. However, a simpler option would be to switch to Object-oriented style. In OOP you call the method on the object passing only the SQL as a single argument.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $pass, $db);
$mysqli->set_charset('utf8mb4');

$mysqli->query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 

Warning!

You should be using parameterized prepared statements instead of manually building your queries. mysqli_query() should only be utilized when you are not passing any inputs to your SQL. Whenever you want to pass inputs, as is the case with INSERT for example, you must use parameter binding. You can replace you mysqli_query() call with:

$stmt = $mysqli->prepare('INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)');
$stmt->bind_param('s', $hits);
$stmt->execute();
like image 38
Dharman Avatar answered Oct 01 '22 14:10

Dharman