我有一个不再有效的登录脚本。它与新的mysqli有关。 所以我将mysql查询更改为mysqli但仍然出错:
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);
时,我不会再收到错误,但我收到了消息:
用户名/密码不正确。
如果正确的话。
我的连接脚本:
<?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");
}
?>
答案 0 :(得分:2)
MySQLI需要参数中的连接和查询。
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
和一个有效的MySQLi Query构造将是:
$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
不正确)编号的行将不< / strong>等于1,但被解释为 0 (Possible Refence)