警告:mysqli_num_rows() 只期望 1 个参数,2 ..在第 14 行


Warning: mysqli_num_rows() expects exactly 1 parameter, 2 .... on line 14

我在下面的脚本中遇到了主题错误:

<?php
include("db.php");
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = mysqli_real_escape_string($link,$_POST['email']);
    $password = mysqli_real_escape_string($link,$_POST['password']);
    $gender = $_POST['gender'];
    $password = md5($password); 
    if ($gender == female) {
        $sql="SELECT id FROM female_users WHERE email='$email' AND password='$password'";
    } else {
        $sql="SELECT id FROM male_users WHERE email='$email' AND password='$password'";
    }
    $result=mysqli_query($link,$sql);
    $count=mysqli_num_rows($link,$result);
    if($count==1) {
        echo('Hello');
    } else  {
        $error="Your Email or Password is invalid";
    }
}
mysqli_close($link);
?>

我不确定它是关于什么的.. 我试图消除 if 语句,但它没有给我任何结果。

要在此处查看错误:

$count=mysqli_num_rows($link,$result);

mysqli_num_rows()函数不将数据库连接作为参数,因此请从那里删除$link,

  • http://php.net/manual/en/mysqli-result.num-rows.php

然后,if ($gender == female) 中的female被视为常量 http://php.net/manual/en/function.constant.php,而不是字符串 http://php.net/manual/en/language.types.string.php:

if ($gender == 'female')

错误报告会给你一些关于一个未定义的恒定女性通知的东西。

  • http://php.net/manual/en/function.error-reporting.php

现在,MD5不再被认为是用于存储密码的安全。

使用以下方法之一:

  • CRYPT_BLOWFISH
  • crypt()
  • bcrypt()
  • scrypt()
  • 在开墙上
  • PBKDF2
  • PBKDF2 on PHP.net
  • PHP 5.5 的password_hash()函数。
  • 兼容包(如果 PHP <5.5)https://github.com/ircmaxell/password_compat/

其他链接:

  • PBKDF2 for PHP

关于列长的重要旁注:

如果您决定使用 password_hash() 或兼容包(如果 PHP <5.5)https://github.com/ircmaxell/password_compat/,请务必注意,如果您当前密码列的长度小于 60,则需要将其更改为该长度(或更高)。手册建议长度为 255。

您需要更改列的长度并使用新的哈希重新开始才能使其生效。否则,MySQL 将以静默方式失败。

试试

 $count=mysqli_num_rows($result);    

将计数查询更改为:

$result=mysqli_query($link,$sql);
    $count=mysqli_num_rows($result);
相关文章: