Just Another Coder
Just Another Coder

Reputation: 3

Issues with Progress Bar

I have this progress bar and what is shown as the description is decided based on has the customer set his targeted reward or not (by checking in database if a value is null) but i set a value to be something else than null and it always prints the "if null" case. Can anybody help?

Code used when creating database:

  target varchar(50) DEFAULT NULL
# I put the current value to be "Bitcoin"

My code that checks the value:

$conn = new PDO($dsn, $username1, $password);
        $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stm_track = $conn->prepare("SELECT target FROM users WHERE username = :username");
        $stm_track->bindParam(":username", $username, PDO::PARAM_INT);
        $stm_track->execute();
        $row = $stm->fetch(PDO::FETCH_ASSOC);
if (is_null($row['target'])) {
        echo "<strong>Bookmark a reward and track your progress!</strong>";
} else {
        echo "<strong>Track your progress for next reward: " . $row['target'] . "</strong>";
}

Upvotes: 0

Views: 63

Answers (1)

GrumpyCrouton
GrumpyCrouton

Reputation: 8621

You declared your fetch() command incorrectly;

$row = $stm->fetch(PDO::FETCH_ASSOC);

Should be

$row = $stm_track->fetch(PDO::FETCH_ASSOC);

These kinds of mistakes are easy to make when you are writing directly to the PDO class, I suggest using a PDO wrapper which handles this more automatically for you.

Upvotes: 1

Related Questions