无法登录:mysqli_query()至少需要2个参数,给定1个 [英] Cant login: mysqli_query() expects at least 2 parameters, 1 given
问题描述
我有一个不再运行的登录脚本.它与新的mysqli有关. 所以我将mysql查询更改为mysqli,但仍然收到错误:
I have a login script that doesn't work anymore. It has something to do with the new mysqli. So I changed mysql query to mysqli but still get errors:
if (empty($errors)){
$query = "SELECT id, username ";
$query .= "FROM users ";
$query .= "WHERE username = '{$username}' ";
$query .= "AND hashed_password = '{$hashed_password}' ";
$query .= "LIMIT 1";
$result_set = mysqli_query($query);
confirm_query($result_set);
if (mysqli_num_rows($result_set) == 1){
$found_user = mysqli_fetch_array($result_set);
$_SESSION['user_id'] = $found_user['id'];
$_SESSION['username'] = $found_user['username'];
redirect_to("faculty.php");
} else {
$message = " Username / Password is incorrect.";
}
那是我的代码,mysqli_query($query);
是问题所在.当我将其更改为mysqli_query($connection , $query);
时,不再收到错误,但收到消息:
That is my code, mysqli_query($query);
is the problem. When I change it to mysqli_query($connection , $query);
, I don't get the error anymore but I get the message:
用户名/密码不正确.
Username / Password is incorrect.
正确的时候.
我的连接脚本:
<?php
require("constants.php");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$connection){
die("Unable to connect to page");
}
?>
<?php
$db = mysqli_select_db($connection , "fm_cms");
if (!$db){
die("Unable to connect to Database");
}
?>
<?php
$db = mysqli_select_db($connection , "fm_cms");
if (!$db){
die("Unable to connect to Database");
}
?>
推荐答案
MySQLI既需要连接也需要参数中的查询.
MySQLI requires both the connection and the query in the parameters.
mysqli_query($con,$query);
完全实用:
$Con = mysqli_connect(/* Connection Information here*/);
$Result_Set = mysqli_query($Con,"SELECT ID FROM table");
为响应您更新的问题正文,当希望更改当前工作模式(数据库)时,将使用mysqli_select_db
.在这种情况下,最初连接到常量DB_NAME
中指定的架构.如果您不想交换架构,但要保持不变.然后删除mysqli_select_db
In response to your updated question body, the mysqli_select_db
is to be used when one wishes to change the current working schema (database). In this case, originally connecting to the schema specified in the constant DB_NAME
. If you are not wanting to swap schemas but keep the same. Then remove the mysqli_select_db
,有效的MySQLi Query构造应为:
and a working MySQLi Query construct would be:
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$Query = mysqli_query($connection,"QUERY HERE");
if (!$connection){
die("Unable to connect to page");
}
并回应; 我不再收到错误,但我收到消息:用户名/密码不正确.正确时..请帮助!
之所以向您显示此消息是因为您已经告诉PHP这样做,因为您的mysqli_num_rows
返回false(由于提供了错误的mysqli_query
),因此编号行将不等于1,但被解释为 0 ((
You are being presented this message because you have told PHP to do so, as your mysqli_num_rows
is returning false (due to a incorrect mysqli_query
being supplied) the numbered rows will not be equal to 1, but be interpreted as 0 (Possible Refence)
这篇关于无法登录:mysqli_query()至少需要2个参数,给定1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!