如何使用Postgresql插件和PHP数据对象(PDO)访问/选择Heroku应用程序上的特定记录属性?

如何使用Postgresql插件和PHP数据对象(PDO)访问/选择Heroku应用程序上的特定记录属性?,php,postgresql,heroku,pdo,Php,Postgresql,Heroku,Pdo,在W3schools.com的以下代码中,需要进行哪些修改才能访问每个返回记录的各个记录属性?也就是说,我不希望更改查询的属性,而是单独访问每个记录的部分以进行输出格式设置 使用MySQLi/MySQLi过程化会容易得多,但我希望调整我的数据库以使用Heroku的PostgreSQL插件 先谢谢你 ''' <!DOCTYPE html> <html> <body> <?php echo "<table style='border: solid

在W3schools.com的以下代码中,需要进行哪些修改才能访问每个返回记录的各个记录属性?也就是说,我不希望更改查询的属性,而是单独访问每个记录的部分以进行输出格式设置

使用MySQLi/MySQLi过程化会容易得多,但我希望调整我的数据库以使用Heroku的PostgreSQL插件

先谢谢你

'''


<!DOCTYPE html>
<html>
<body>

<?php
echo "<table style='border: solid 1px black;'>";
 echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
        echo "<tr>";
    }

    function endChildren() {
        echo "</tr>" . "\n";
    }
}

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

</body>
</html>