I am unable to call user information

Asked

Viewed 82 times

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

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

Does anyone know how I fix?

  • you need to call the check() before : echo 'Você é '. User::$Row['username'];

  • Do it like this: User::Check(); echo User::$Row['username'];

  • 1

    thanks rray, you saved my life!

  • Please avoid long discussions in the comments; your talk was moved to the chat

2 answers

1

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

index php.

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

However $Row will only have its value set after the method call Check(). The adjustment of the index was:

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

0

I think the problem is in attributing the values to the SELECT. I usually use the bindValue to set 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;
        }
    }
?>
  • guess the problem is q n is recognizing $Row from User.php

  • test the contents of the function outside the class, try to print the values that come from the bank to be sure that is no problem with the SELECT or something like.

  • I tried to print the content in index.php with echo User::$Row['username']; only q gave error Notice: Undefined

Browser other questions tagged

You are not signed in. Login or sign up in order to post.