Bruce Bruce - 3 months ago 12
MySQL Question

PHP PDO MYSQL - variable always null

There are two queries in the code below both using the exact same variables of $data and $username. The second runs and updates in the database perfectly. The first however, always returns NULL and there is no logic to it that I can see. The status row does exist in the database, it is in fact set and can't be NULL. NULL is not even an option for that cell, the default value is set to "unopened". What am I missing?

if (isset($_POST['ids'])) {

$data = $_POST['ids'];
$query = $db->prepare("SELECT status FROM email WHERE mailto=:username AND id=:id");
$query->bindValue(':username', $username);
$query->bindValue(':id', $data);
$query->execute();
$thestatus = $row['status'];

echo $thestatus;

$query = $db->prepare("UPDATE email SET status='opened' WHERE mailto=:username AND id=:id");
$query->bindValue(':username', $username);
$query->bindValue(':id', $data);
$query->execute();
}

Answer

You need to perform $row = $query->fetch(PDO::FETCH_ASSOC); after executing the statement to fetch the resultset row