PHP PDO 没有在我的网页上显示任何数据

PHP PDO is not displaying any data on my web page

我最近尝试将程序 MySQL 查询转换为 PDO 语句。我从 php 官方文档中复制了以下代码,并向其中添加了我的参数。它没有在页面中显示任何结果。

<?php
$dsn = 'mysql:host=localhost;dbname=database';
$user = 'user';
$pass = 'pass';
try {
    $dbh = new PDO($dsn , $user, $pass);
    $dbh = null;
} catch (PDOException $e) {
    print "An error has occurred. Please contact support. <br/>" . $e->getMessage() . "<br/>";
    die();
}

$value = 'user1';

$stmt = $dbh->prepare("SELECT * FROM table where username = ?");
if ($stmt->execute(array($value))) {
    while ($row = $stmt->fetch()) {
        print_r($row);
}

?>
    Try this:-

    <?php

    $dsn = 'mysql:host=localhost;dbname=databasename';

    $user = 'user';

    $pass = 'password';

    try {
        $dbh = new PDO($dsn , $user, $pass);

    } catch (PDOException $e) {

        print "An error has occurred. Please contact support. <br/>" .

     $e->getMessage() . "<br/>";

        die();

    }

    $value = 'user1';

    $stmt = $dbh->prepare("SELECT * FROM table where column= ?");
   if ($stmt->execute(array($value))) {
    while ($row = $stmt->fetch()) {
        print_r($row);
    }
}
    ?>