<?php
                if($sth1->rowCount() > 0) {
                    $row = $sth1->fetch(PDO::FETCH_ASSOC);
                    echo "<div> <h2>{$row1['prefix']} {$row1['code']}</h2></div>";
                    } else {
                    echo "No results.";
                }
            unset($sth1);
            ?>


<?php
$username = "###";
$password = "####";
$pdo1 = new PDO('mysql:host=####;dbname=####', $username, $password);
$pdo1->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth1 = $pdo1->prepare('SELECT pID, lname, fname FROM Professor ORDER BY pID DESC LIMIT 10;;');
$sth1->execute(array());
?>


结果:只是空白页,没有错误。

最佳答案

在第2行中使用时,未设置变量$ sth1。尝试以下操作(更改属性/表名称):

ini_set("display_errors", TRUE);
error_reporting(E_ALL);

$pdo1 = new PDO('mysql:host=localhost;port=8889;dbname=test', '**', '**' );
$pdo1->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth1 = $pdo1->prepare('SELECT name, title FROM tab1 ORDER BY name DESC LIMIT 10');
$sth1->execute(array());

if($sth1->rowCount() > 0) {

    $row = $sth1->fetch(PDO::FETCH_ASSOC);
    echo "<div> <h2>{$row['name']} : {$row['title']}</h2></div>";

} else {

    echo "No results.";

}

10-08 13:33