mysqli_stmt :: fetch()期望恰好0个参数,其中1个给定

时间:2019-04-07 15:29:47

标签: sql mysqli pdo

我尝试从数据库中选择用户名,然后在获取用户名时收到错误消息。

  

mysqli_stmt :: fetch()期望恰好0个参数,其中1个是

<?php

require 'dbh.inc.php';

if(!empty($_POST['username']) && !empty($_POST['password'])):

    $records = $conn->prepare('SELECT id, username, password FROM users WHERE username = ?');
    $records->bind_param('s', $_POST['username']);
    $records->execute() or die($records->error);
    $result = $records->fetch(PDO::FETCH_ASSOC);

    if(count($result) > 0 && password_verify($_POST['password'], $result['password'])){
        die('We have a login');
    }else{
        die('Something went wrong!');
    }

endif;

?>

2 个答案:

答案 0 :(得分:0)

您似乎正在调用mysqli_stmt :: fetch(),但希望它的行为像PDOStatement::fetch()

即使这些函数具有相同的名称,它们也来自不同的扩展名,并且您无法将它们混合使用。如果要使用PDO fetch(),则应使用PDO连接到数据库。

此外,mysqli_stmt::fetch()函数不会返回数据行。它取决于您binding results to variables

我更喜欢PDO样式,其中PDOStatement::fetch()返回一行。我建议您将代码转换为始终使用PDO。

答案 1 :(得分:0)

您正在通过fetch() MySQLi方法使用PDO提取样式。

尝试一下:

$result = $records->fetch();

如果您真的想使用PDO提取样式,建议您使用PDO代替MySQLi。

相关问题