PDO foreach循环-PHP

时间:2020-08-15 13:10:45

标签: php pdo

我只是想为我的网站制作一个简单的API。

    $stmt = $pdo->prepare('SELECT id, name FROM locations WHERE id = ?');
    $stmt->execute([$_GET['location']]);
    $locations = $stmt->fetch(PDO::FETCH_ASSOC);
    $location = $locations['name'];
    $data = array();
    //Fetch records data
    $stmt = $pdo->prepare('SELECT studentIcNo, status, time, temperature FROM records WHERE locationID = ? AND date = ?');
    $stmt->execute([$_GET['location'], $_GET['date']]);
    while($records = $stmt->fetchAll(PDO::FETCH_ASSOC)){ 
        foreach($records as $record){
            $gender = 'N/A';
            $recordsData = array(array(
                "name" => $record['studentIcNo'],
                "gender" => $record['status'],
                "status" => $gender,
                "time" =>   $record['time'],
                "temperature" => $record['temperature']
            ));
        }
        $data = array(
            "location" => $location,
            "date" => $_GET['date'],
            "records" => $recordsData
        ); 
    };
    echo json_encode($data);  

以某种方式它不会为JSON数据的records对象创建新数组。抱歉,我对这件事有点陌生。我相信我的循环方法在某种程度上不是最好的方法,也不起作用。出于测试目的,$gender变量设置为N/A(在我实际上从另一个表获取数据之前)。 感谢您的答复

0 个答案:

没有答案
相关问题