G
G
glem13372020-04-18 12:52:12
PHP
glem1337, 2020-04-18 12:52:12

Undefined index in array from PDO fetch?

I make a query like

$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
    $hero->execute(['nickname' => $nickname]);
    $hero = $hero->fetch();


If you look through var_dump, then everything is ok. As soon as I try to display, for example, $hero['real_name'], some elements give an error. Database connection settings:
$host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'superheroes';
    $charset = 'utf8';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES   => false);

    $pdo = new PDO($dsn, $user, $pass, $opt);

    return $pdo;


What could be the problem? In encoding? DB encoding utf8_general_ci.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question