0

I have been able to query the data from the MySQL database and to put the array into a variable $results, however I am not able to put a part of the array, like the password or the regtime in the $results varible into separate variables. What am I doing wrong?

This is my select function in my databse class where I am querying the data from the database using a perameter of $query. This works for my posts page, so i dont think this is the problem.

// Select Function
        
        public function select($query){
            $result = $this->connection->query($query);
            
            while ( $obj = $result->fetch_object()){
                $results[] = $obj;
            }
            
            return $results;
        }

This is my login function in my query class where I am putting the submitted username into the query and then passing it into the select function above to pull the results with that username, which is returned to the login function and stored in the $results variable. It then checks whether the variable is empty and if it is it kills the script, this is all working as I get: 1)yay!!!! 2)and what is displayed by print_r() which is:

Array ( [0] => stdClass Object ( [ID] => 1 [username] => dillon [fname] => Dillon [lname] => Erhardt [email] => [email protected] [password] => dilandsas [regtime] => 1453685794 ) ) 

function login($redirect) {
			global $db;
            
            if(!empty($_POST)) {
                
                $username = htmlspecialchars($_POST['username']);
                $password = htmlspecialchars($_POST['password']);
                
                $query = "
                      SELECT * FROM users
                      WHERE username = '$username'
                    ";
            
                $results = $db->select($query);


				//Kill the script if the submitted username doesn't exit
				if (empty($results)) {
					die('Sorry, that username does not exist!');
				} 
                
                echo "yay";
                print_r($results);


				//The registration date of the stored matching user
				$storeg = $results['regtime'];

				//The hashed password of the stored matching user
				$stopass = $results['password'];

The problem is that when i try and store the vaule from the password or regtime results in my $storeg and $stopass variables using $results['password']; or $results['regtime']; I get these notices

Notice: Undefined index: regtime in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 55

Notice: Undefined index: password in /Applications/MAMP/htdocs/Test/CMS-v2/includes/class-query.php on line 58
Invalid Login

How do I store the password and regtime values of from the array into the $results variable?

1
  • Will you please list your your 'user' tables fields? Commented Jan 27, 2016 at 11:36

2 Answers 2

1

Try the following code to do so.

function login($redirect) {
            global $db;

            if(!empty($_POST)) {

                $username = htmlspecialchars($_POST['username']);
                $password = htmlspecialchars($_POST['password']);

                $query = "
                      SELECT * FROM users
                      WHERE username = '$username'
                    ";

                $results = $db->select($query);


                //Kill the script if the submitted username doesn't exit
                if (empty($results)) {
                    die('Sorry, that username does not exist!');
                } 

                echo "yay";
                print_r($results);


                //The registration date of the stored matching user
                $storeg = isset($results[0]->regtime) ? $results[0]->regtime : "";

                //The hashed password of the stored matching user
                $stopass = isset($results[0]->password) ? $results[0]->password : "";
Sign up to request clarification or add additional context in comments.

Comments

0

In your select, you fetch an objects from the result set and add this to an array (result).

Accordingly, you need to grab the object from the result array and reference a property within that:

$storeg = $results[0]->regtime;

This will obviously only work, if $result contains one and exactly one row from the DB. All other cases (no row, three rows) will cause errors due to missing error handling.

1 Comment

Thank you. Thats exactly what i needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.