mysqli_query expects at least 2 parameters

2018-12-31 00:49发布

This mysqli_query command results in the error below

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

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

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

标签: php mysql mysqli
5条回答
查无此人
2楼-- · 2018-12-31 01:21

From the manual

Procedural style

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

You'll notice the $link and $query variables.

This means that you need to pass the function a valid mysqli link resource as well as the query you wish to perform. This lets the function know which established connection to the server to use.

A link resource can be created using:

Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

and an example of how to do so can be found on the aforementioned manual page.

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 01:29
    <?php
include_once('insert.php');
if(isset($_POST['submit']))
{
     $name = $_POST['name'];
     $email = $_POST['email'];
     $address = $_POST['address'];
     $adm= $_POST['admission'];
    mysqli_query("INSERT INTO students_recrod(name,email,address,joining_date) VALUES ('$name', '$email','$address', '$adm')");
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Sign up form</title>
</head>
<body>
<form action="" method="post" id="">
<label>Name</label><br>
<input type="text" name="name" id=""><br>

<label>Email</label><br>
<input type="text" name="email" id=""><br>

<label>Address</label><br>
<input type="text" name="address" id=""><br>

<label>Admission date</label><br>
<input type="text" name="admission" id=""><br><br>

<input type="submit" name="submit" id="">
</form>
</body>
</html>
查看更多
君临天下
4楼-- · 2018-12-31 01:30

you need to specify the connection that you made to your database somewhere earlier in your page. you should put that variable in the query. Suppose you made a variable called $con. Then your code should be like this.

mysqli_query($con,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES ('".$hits."')"); 
查看更多
旧时光的记忆
5楼-- · 2018-12-31 01:33

mysqli_query excepts 2 parameters , first variable is mysqli_connect equivalent variable , 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 ('".$hits."')");
查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 01:41

It seems you are confusing mysql_query with mysqli_query. The former accepts the sql statement as the first param, while the latter expects a link identifier (created by Mysqli::connect) as the first param and the statement as the second.

The two extensions are not compatable with each other. I suggest you pick one, read the manual pages on how to connect, execute queries, etc, and forget the other exists. which one you pick is up to you, mysqli is more feature rich but more complicated as a result.

查看更多
登录 后发表回答