I'm not sure what's wrong with the following code: It doesn't throw an error but it also doesn't return any actual data.
If it's running the query that I want it to (
SELECT * FROM heroes WHERE year_month = 2
), it should return data.When I run that same query in phpMyAdmin it returns a record.
So a logical first step is to verify that the query is correct. Is it possible to display the actual query that's being executed including the variable parameters? If not, does anyone see other obvious issues?
$host = 'localhost';
$db = 'hero_images';
$user = 'sqlmin';
$pass = '***********';
$charset = 'utf8mb4';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass, $options);
} catch (PDOException $e) {
echo $e->getMessage()."<br>";
die();
}
$id = 2;
$sql = "SELECT * FROM `heroes` WHERE `year_month`=:year_month";
$stmt = $pdo->prepare($sql);
$stmt->execute(['year_month' => $id]);
$row = $stmt->fetch(PDO::FETCH_NUM);
echo "<br />row['h1_website'] = ".$row['h1_website'];
echo "<br />row['h1_tablet'] = ".$row['h1_tablet'];
echo "<br />row['h1_mobile'] = ".$row['h1_mobile'];
$pdo = null;
Thanks in advance...
Paul