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?