Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc() [duplicate]



PHP Snippet 1:

<?php
include 'db-conn.php';
$post_username = $_POST['post_username'];
$post_password = $_POST['post_password'];

$sql = "SELECT username, password FROM users WHERE username = ? AND password = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $post_username, $post_password);
$stmt->execute();

// bind variables to the resulted values
$stmt->bind_result($u, $p);

while ($row = $stmt->fetch()) {
    echo "Username = $u and Password = $p <br>";
}

PHP Snippet 2:

<?php
include 'db-conn.php';
$post_username = $_POST['post_username'];
$post_password = $_POST['post_password'];

$sql = "SELECT username, password FROM users WHERE username = ? AND password = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $post_username, $post_password);
$stmt->execute();

// convert to a result
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo "Username = $row[username] and Password = $row[password] <br>";
}