我可以列出用户,但是我想显示用户当天的登录时间
$stmt = $conn->prepare("select id,username,date from user_tbl");
$stmt->execute();
$row = $stmt-> fetchAll(PDO::FETCH_ASSOC);
$user_arr=array('data'=>$row);
echo json_encode($user_arr);
基于用户ID,我想不计入log_history表中的登录数,如何在PDO json编码中使用以下查询。
select count(id) as count1 from log_history where user_id=".$row['id']."
我认为,您想要这样的输出:
------------------------------------------
id | username | date | login count
------------------------------------------
1 | user1 | 20-10-1019 | 20
------------------------------------------
2 | user2 | 20-10-1019 | 25
------------------------------------------
而且,我没有运行代码。因此,代码可能需要进行一些修改才能正常运行。
所以,您需要运行一个循环:
$ stmt = $ c
$conn->prepare("select id,username,date from user_tbl");
$stmt->execute();
$row = $stmt-> fetchAll(PDO::FETCH_ASSOC);
$users = [];
foreach ($row as $item){
$count_sql = "select count(id) as count1 from log_history where user_id=".$item['id'];
$stmt = $conn->prepare();
$stmt->execute();
$count_row = $stmt-> fetchAll(PDO::FETCH_ASSOC);
$users[] = [ 'user' => $item, 'log_count' => $count_row['log_count'] ]
}
var_dump($users);