php mysqli_query 需要至少 2 个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8073278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysqli_query expects at least 2 parameters
提问by Joe Winfield
This mysqli_query
command results in the error below
此mysqli_query
命令导致以下错误
mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES ('".$hits."')");
Warning: mysqli_query() expects at least 2 parameters, 1 given in
警告:mysqli_query() 需要至少 2 个参数,其中 1 个在
What does this error message mean, and how can it be fixed?
此错误消息是什么意思,如何修复?
回答by RonnyRules
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.
您需要在页面前面的某个位置指定与数据库建立的连接。您应该将该变量放入查询中。假设您创建了一个名为 $con 的变量。那么你的代码应该是这样的。
mysqli_query($con,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES ('".$hits."')");
回答by Ben Swinburne
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.
您会注意到$link
和$query
变量。
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.
这意味着您需要向函数传递一个有效的 mysqli 链接资源以及您希望执行的查询。这让函数知道要使用哪个与服务器建立的连接。
A link resource can be created using:
可以使用以下方法创建链接资源:
Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()
仅程序样式:由 mysqli_connect() 或 mysqli_init() 返回的链接标识符
and an example of how to do so can be found on the aforementioned manual page.
可以在上述手册页上找到如何执行此操作的示例。
回答by erm410
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.
看来您将 mysql_query 与 mysqli_query 混淆了。前者接受 sql 语句作为第一个参数,而后者需要一个链接标识符(由 Mysqli::connect 创建)作为第一个参数,语句作为第二个参数。
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.
这两个扩展彼此不兼容。我建议您选择一个,阅读有关如何连接、执行查询等的手册页,而忘记另一个存在。你选择哪一个取决于你,mysqli 功能更丰富,但结果也更复杂。
回答by Dharman
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
mysqli_query() 需要至少 2 个参数,1 个给定
mysqli_select_db() 需要 2 个参数
mysqli_real_escape_string() 需要 2 个参数
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.
这意味着您没有将强制参数传递给这些函数。MySQLi 过程式函数期望第一个参数是有效的 MySQLi 连接链接。
For example, mysqli_query
expects a database link as the first argument and the actual SQL query as a second argument.
例如,mysqli_query
将数据库链接作为第一个参数,将实际 SQL 查询作为第二个参数。
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
并将其作为第一个参数传递。
mysqli_query($mysqli, "INSERT INTO `counter`.`hits` (`page_hits`) VALUES ('".$hits."')");
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 过程函数需要将连接作为参数传递。然而,一个更简单的选择是切换到面向对象的风格。在 OOP 中,您调用对象上的方法,仅将 SQL 作为单个参数传递。
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 ('".$hits."')");
Warning!
警告!
You should be using parameterized prepared statementsinstead 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:
您应该使用参数化准备好的语句,而不是手动构建查询。mysqli_query()
仅当您不向 SQL 传递任何输入时才应使用。每当您想传递输入时,INSERT
例如,就必须使用参数绑定。您可以将您的mysqli_query()
电话替换为:
$stmt = $mysqli->prepare('INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)');
$stmt->bind_param('s', $hits);
$stmt->execute();
回答by Shanaka WickramaArachchi
mysqli_query
excepts 2 parameters , first variable is mysqli_connect
equivalent variable , second one is the query you have provided
mysqli_query
除了2个参数,第一个变量是mysqli_connect
等效变量,第二个是您提供的查询
$name1 = mysqli_connect(localhost,db_username ,db_pswd ,db_name );
$name2 = mysqli_query($name1,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES ('".$hits."')");