开发者

Why isnt this pulling anything? Php/mysql

开发者 https://www.devze.com 2023-02-27 15:31 出处:网络
<?php if($sth1->rowCount() > 0) { 开发者_JS百科$row = $sth1->fetch(PDO::FETCH_ASSOC); echo \"<div> <h2>{$row1[\'prefix\']} {$row1[\'code\']}</h2></div>\";
            <?php
                if($sth1->rowCount() > 0) {
                    开发者_JS百科$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());
?>

Result: Just a blank page, no error.

Why isnt this pulling anything? Php/mysql


I hope your two code blocks are the other way around in your php file...

Apart from that you are using $row1 in your echo statement but your data is stored in variable $row. The blank page would then be a div with an empty h2.


Variable $sth1 isn't set, when it is used in line 2. Try this one (changed attributes/name of table):

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.";

}
0

精彩评论

暂无评论...
验证码 换一张
取 消