0

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

Realto619
  • 83
  • 2
  • 7
  • [PDO fetch modes: PDO::FETCH_ASSOC](https://phpdelusions.net/pdo/fetch_modes#FETCH_ASSOC) – Your Common Sense Feb 22 '18 at 20:06
  • How is this duplicate? He want to know how the sql query looks like. This isn't described on the linked answer. – Sebastian Brosch Feb 22 '18 at 20:08
  • Okay, can we ignore the "how can I view the actual query" part of my question and shift the focus to anyone spotting any errors in the code or should I create a new question entirely? – Realto619 Feb 22 '18 at 20:45

0 Answers0