0

I try to select username from the database, and when I fetch it, I got the error message.

mysqli_stmt::fetch() expects exactly 0 parameters, 1 given in

<?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;

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

You appear to be calling mysqli_stmt::fetch(), but expecting it to act like PDOStatement::fetch().

Even though the functions have the same name, they come from different extensions, and you can't mix them. If you want to use the PDO fetch(), you should connect to your database using PDO.

Also, the mysqli_stmt::fetch() function does not return a row of data. It relies on you binding results to variables.

I prefer the PDO style, where PDOStatement::fetch() returns a row. I recommend you convert your code to use PDO throughout.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

You are using PDO fetch style with the fetch() MySQLi method.

Try this :

$result = $records->fetch();

If you really want to use the PDO fetch style, I recommend to use PDO instead of MySQLi.

Arnaud Peralta
  • 1,287
  • 1
  • 16
  • 20