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

时间:2015-03-23 12:06:08

标签: php mysql pdo

我最近试图将我的程序化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);
}

?>

1 个答案:

答案 0 :(得分:2)

    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);
    }
}
    ?>