php - What is the proper way to bind to different values in PDO equal to mysqli?

Solution:

There's no equivalent of bind_result in PDO because you don't really need it. Just read the data from the row:

if ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $_SESSION['id'] = $row["id"];
    $_SESSION['name'] = $row["name"];
    $is_valid = true;
}

You also don't need the $stmt->bindParam(':name', $name); line because there is no :name input parameter in your SQL.

More examples are available in the manual and elsewhere.

See also Is it possible to use store_result() and bind_result() with PHP PDO? for more useful background info.

Answer

Solution:

The equivalent method is called . You can bind a variable to one column in the result set.

/* Bind by column number */
$stmt->bindColumn(1, $id);
$stmt->bindColumn(2, $name);

while ($stmt->fetch(PDO::FETCH_BOUND)) {
    print $name . "\t" . $id. "\n";
}

However, I would recommend writing simpler code. PDO is designed to be easier to use.

If you want to make the code simpler, use arrays. The method returns an array with the current row. They are better when you need to fetch more than one column from the result. If you only need to fetch one column, use .

$sql = "SELECT id, name FROM users WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->execute([
    'id' => $id,
    'name' => $name,
]);

if ($row = $stmt->fetch()) {
    $_SESSION['id'] = $row['id'];
    $_SESSION['name'] = $row['name'];
    $is_valid = true;
} else {
    $is_valid = false;
    self::logout();
}

Source