MiniGunnR
MiniGunnR

Reputation: 5800

PHP PDO is not displaying any data on my web page

I've recently tried to convert my procedural MySQL queries to PDO statements. I've copied the following code from php official documentation and added my parameters to it. It is not showing any results in the page.

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

?>

Upvotes: 2

Views: 387

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

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

Upvotes: 2

Related Questions