我有一个小脚本,它从我的客户端获取用户名和密码,如果他们的凭据是正确的,他们将被带到我的开发服务器上的他们的站点目录。
if
条件完美无缺,并响应与我的MySQL数据库中的计数器部分匹配的正确凭据。
但是,else
条件根本不会执行(即,如果用户的凭据是incorret,则不会将用户定向到错误页面)。我认为while
循环可能会使逻辑跳闸,但我需要$row
变量来从DB获取目录字段。
有什么想法吗?
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
$pass = md5($password);
$conn = mysqli_connect("localhost", "root", "", "digital_server");
$sql = "SELECT * FROM projects WHERE username = '$username' AND password = '$pass'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)) {
if($query) {
header("Location: " . $row['directory'] . "/");
}
else {
header("Location: http://dev.myserver.com/error.php");
}
}
分辨 非常感谢所有帮助过的人。
修改后的代码块
删除了while并创建了一个$row
变量来获取if
逻辑所需的数据
$row = mysqli_fetch_assoc($query);
if(mysqli_num_rows($query) > 0) {
header("Location: " . $row['directory'] . "/");
}
else {
header("Location: http://dev.myserver.com/error.php");
}
答案 0 :(得分:1)
// No need of while here
//while() {
if( mysqli_num_rows($query) > 0 ) {
$row = mysqli_fetch_assoc($query);
header("Location: " . $row['directory'] . "/");
} else {
header("Location: http://dev.myserver.com/error.php");
}
选项2
$sql = "SELECT * FROM projects WHERE username = '$username' AND password = '$pass'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);
if(count($row ) > 0 ) {
header("Location: " . $row['directory'] . "/");
} else {
header("Location: http://dev.myserver.com/error.php");
}
答案 1 :(得分:0)
您可能正在寻找mysqli_num_rows($result);
$query = mysqli_query($conn, $sql);
if( mysqli_num_rows($query) > 0 ) {
$row = mysqli_fetch_assoc($query);
header("Location: " . $row['directory'] . "/");
}else{
header("Location: http://dev.myserver.com/error.php");
}
您始终可以使用查询来捕获查询中的错误 mysqli_error
答案 2 :(得分:0)
$username = stripslashes($_POST['username']);
$password = stripslashes($_POST['password']);
$pass = md5($password);
$conn = mysqli_connect("localhost", "root", "", "digital_server");
$sql = "SELECT * FROM `projects` WHERE `username` = '$username' AND `password` = '$pass'";
$query = mysqli_query($conn, $sql);
if( mysqli_num_rows($query) > 0 ) {
$row = mysqli_fetch_assoc($query);
header("Location: " . $row['directory'] . "/");
}
else
{
header("Location: http://dev.myserver.com/error.php");
}