I can not call user information [closed]

2

User.php

<?php
    class User {
        public static $Row = [];

        public static function Check() {
            global $PDO;

            $usersql = $PDO->prepare("SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1");
            $usersql->execute(array(':username' => $_SESSION['username'], ':password' => $_SESSION['password']));

            $Row = $usersql->fetch(PDO::FETCH_ASSOC);

            self::$Row = $Row;
        }
    }

Index.php

echo 'Você é '. User::$Row['username'];

And error that gives:

  

Notice: Undefined index: username in C: \ xampp \ htdocs \ Index.php on line 1

It appears in index.php in Check() , when I try to call user information it gives this error.

Does anyone know how to solve it?

    
asked by anonymous 29.09.2015 / 15:29

2 answers

1

By the comments and the Undefined index: username in error message, it was concluded that the problem was in the index.php file that looked like this:

index.php

<?php
   require 'User.php';
   echo 'Você é '. User::$Row['username'];

But $Row will only have its value set after calling the Check() method. The index setting was:

<?php
   require 'User.php';
   User::Check(); //método que define o valor de $Row
   echo 'Você é '. User::$Row['username'];
    
30.09.2015 / 01:20
0

I think the problem is in assigning the values to SELECT . I usually use bindValue to set the values to the SQL string. Try this:

<?php
    class User {
        public static $Row = [];
        public static function Check() {
            global $PDO;

            $usersql = $PDO->prepare("SELECT * FROM users WHERE username = :username AND password = :password LIMIT 1");
            $usersql->bindValue(":username", $_SESSION['username']);
            $usersql->bindValue(":password", $_SESSION['password']);
            $usersql->execute();

            $Row = $usersql->fetch(PDO::FETCH_ASSOC);

            self::$Row = $Row;
        }
    }
?>
    
29.09.2015 / 16:18