My PHP script obtains a list of unique users and their last entries in my database. My goal is to take this list of users and use it in my javascript code.
I've checked my PHP code and it prints out the list of users. However, when I use JSON to get the data over to javascript, an entirely different column of data is shown!
I can even remove json_encode from my php and remove that column of data from my SQL query and it still shows up. I just started using PDO so I think thats the culprit. Any help?
PHP Code Snippet:
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Get all unique users last event.
$sql = ("SELECT * FROM events WHERE Id in (SELECT MAX(Id) FROM events GROUP BY UserName)");
$stmt = $dbh->prepare($sql);
if($stmt->execute(array($_GET['Region']))){
while($row = $stmt->fetch()){
//If most recent event is login or enter, user is currently logged in
if($row['Type'] == "logout" || $row['Type'] == "login"){
array_push($userList, $row['UserName']);
$currentUserCount++;
}
}
}
echo("Number of Users Logged in: " . $currentUserCount);
echo json_encode($userList);
}
JS Code Snippet:
<script type="text/javascript">
var userList = [];
$(document).ready(function(){
$.getJSON('/Statistics/process.php', function(data) {
$.each(data, function(key){
userList.push(key);
document.write(key + "<br />");
});
});
console.log(userList);
});
</script>
Extra Info:
My DB has this set up:
Id | UUID | UserName | Region | Type | Time |
I am sending the UserName data but receiving the Region data in js.