This mysqli_query
command results in the error below
mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)");
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();