Undefined index in the array of sample PDO?

Doing the query
$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 you try to deduce, for example, $hero['real_name'],some items give an error. Setup the DB connection:
$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 the encoding? Database encoding utf8_general_ci.
April 19th 20 at 12:40
0 answer

Find more questions by tags PHP