douxiong2999 2014-07-03 01:09
浏览 50

使用JSON从MySQL数据库获取的数据错误

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘