I'm trying to create a global variable which contains user specific data from my database. I've been looking over the internet but I couldn't find the right answer. I'm not quite sure how to approach this.
At this point of code, the user has been registered and logged in and his login data is saved inside $s_email and $s_password, which are session variables.
Here's some code (mysql.php) which contains the mysql class:
class mysql {
// Create database connection
private $db;
function __construct() {
$this->db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or
die('Database fout.');
}
// Receive user data based on cookie, then fetch this data into an array
function getUserData($s_email, $s_password, $data){
$sql = "SELECT
`id`,
`firstname`,
`lastname`,
`city`,
`country`,
`gender`,
`bio`,
`active`,
`member_since`
FROM `users`
WHERE email = '$s_email'
AND password ='$s_password'";
// perform the query and store the result
$result = $this->db->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
$data = array();
global $data;
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
}
}
And this is my profile.php which will try to run the function (the echo $data[''] does not work, but that's the kind of approach I would like to take on this system):
<?php
// Receive user data and send it to mysql class
$mysql->getUserData($s_email, $s_password, $data);
echo $data['email'];
?>
Eventually I'm trying to create a simple access method to the array variables.
NOTE: I've only been scripting PHP and MySQL for 2 days, so any advice on my code is really appreciated.
Thank you,