$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
$hero->execute(['nickname' => $nickname]);
$hero = $hero->fetch();
$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;
$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
$hero->execute(['nickname' => $nickname]);
$hero = $hero->fetch();
$var_dump($hero);
$var_dump($hero['real_name']); // и вот тут "undefined" ?
<?php
//если вывожу вот так, то ошибка
var_dump($data['real_name']);
// если так, то выводит
var_dump($data);
?>
array(6) {
["id"]=>
int(1)
["nickname"]=>
string(8) "Superman"
["real_name"]=>
string(10) "Clark Kent"
["origin_description"]=>
string(158) "he was born Kal-El on the planet Krypton, before being rocketed to
Earth as an infant by his scientist father Jor-El, moments before Krypton's destruction..."
["superpowers"]=>
string(109) "solar energy absorption and healing factor, solar flare and heat vision,
solar invulnerability, flight..."
["catch_phrase"]=>
string(68) "“Look, up in the sky, it's a bird, it's a plane, it's Superman!”"
}
<?php
class Controller_Hero extends Controller
{
function __construct()
{
$this->model = new Model_Hero();
$this->view = new View();
}
function action_index()
{
}
function action_view($nickname)
{
$data = $this->model->get_hero($nickname);
$this->view->generate('hero_view.php', 'template_view.php', $data);
}
function action_edit ($nickname) {
$data = $this->model->get_hero($nickname);
$this->view->generate('hero_edit_view.php', 'template_view.php', $data);
}
function action_save ($nickname) {
$data = $this->model->save_hero($nickname);
$this->view->generate('hero_edit_view.php', 'template_view.php', $data);
}
}
?>
<?php
class Model_Hero extends Model
{
public function get_hero($nickname)
{
$pdo = Database::DataBaseConnect();
$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
$hero->execute(['nickname' => $nickname]);
$hero = $hero->fetch();
$pdo = null;
return $hero;
}
}
<pre>
<?php
var_dump($data['real_name']);
?>
</pre>
<?php
class Database {
static public function DataBaseConnect()
{
$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;
}
}
<?php
var_dump($data);
?>
<?php
var_dump($data['real_name']);
?>
array(6) {
["id"]=>
int(1)
["nickname"]=>
string(8) "Superman"
["real_name"]=>
string(10) "Clark Kent"
["origin_description"]=>
string(158) "he was born Kal-El on the planet Krypton, before being rocketed to
Earth as an infant by his scientist father Jor-El, moments before Krypton's destruction..."
["superpowers"]=>
string(109) "solar energy absorption and healing factor, solar flare and heat vision,
solar invulnerability, flight..."
["catch_phrase"]=>
string(68) "“Look, up in the sky, it's a bird, it's a plane, it's Superman!”"
}
Notice: Undefined index: real_name in C:\OSPanel\domains\superheroes\application\views\hero_edit_view.php on line 11
NULL
$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
$hero->execute(['nickname' => $nickname]);
$hero = $hero->fetch();
// вот эти строки добавь прямо в текст файла, где выполняется запрос
$var_dump($hero);
$var_dump($hero['real_name']); // и вот тут "undefined" ?
die();
$hero_images = $pdo->prepare('SELECT path FROM photos WHERE id_hero = :id');
$hero_images->execute(['id' => $hero['id']]);
$hero_images = $hero_images->fetchAll();
$hero['images'] = $hero_images;
$pdo = Database::DataBaseConnect();
$hero = $pdo->prepare('SELECT * FROM heroes WHERE nickname = :nickname');
$hero->execute(['nickname' => $nickname]);
$hero = $hero->fetch();
var_dump($hero);
var_dump($hero['real_name']); // и вот тут "undefined" ?
die();
array(6) { ["id"]=> int(1) ["nickname"]=> string(8) "Superman" ["real_name"]=> string(10) "Clark Kent" ["origin_description"]=> string(158) "he was born Kal-El on the planet Krypton, before being rocketed to Earth as an infant by his scientist father Jor-El, moments before Krypton's destruction..." ["superpowers"]=> string(109) "solar energy absorption and healing factor, solar flare and heat vision, solar invulnerability, flight..." ["catch_phrase"]=> string(68) "“Look, up in the sky, it's a bird, it's a plane, it's Superman!”" }
Notice: Undefined index: real_name in C:\OSPanel\domains\superheroes\application\models\model_hero.php on line 15
NULL