gpt4 book ai didi

php - 输出数据库列中的记录数并将其分配为变量

转载 作者:行者123 更新时间:2023-11-30 22:37:34 24 4
gpt4 key购买 nike

我想在我当前的查询中计算我的用户表中的 id 字段并输出它。我如何更改我的查询或我的输出,以便如果我的用户表中有 40 个 id 具有相同的 WHERE 条件适用,我可以用一个变量输出它?

$member = mysqli_query($con,"SELECT * FROM users WHERE `group` IN (2, 3, 4, 5) ORDER BY id DESC LIMIT 1");
$numrows_member = mysqli_num_rows($member);
if($numrows_member > 0){
while($row_member = mysqli_fetch_assoc($member)){
$memberid = $row_member['id'];
$member_username = $row_member['username'];
}
} else {
$no_members = "No Members Found...";
}

最佳答案

试试这段代码:

警告:还要重命名组列,因为在示例中组是 mysql 数据库的保留关键字,我将其重命名为 groupid。

<?php
$member = mysqli_query($con,"SELECT * FROM users WHERE `groupid` IN (2, 3, 4, 5) ORDER BY id DESC LIMIT 1");

$numrows_member = mysqli_num_rows($member);

if($numrows_member > 0){
$totalRecord=null;
while($row_member = mysqli_fetch_assoc($member)){
$memberid = $row_member['id'];
$member_username = $row_member['username'];

$group_id = intval($row_member['groupid']);
if($group_id > 0){
$totalRecord = getTotalRow($con,$group_id);
}
}
echo 'ID : '.$memberid.' UserName : '.$member_username. ' Total records : '.$totalRecord;
} else {
echo $no_members = "No Members Found...";
}

function getTotalRow($con,$id){
$record=0;
$result = mysqli_query($con,"SELECT count(id) as total FROM users WHERE groupid = $id ");
while($row = mysqli_fetch_row($result)){
echo $record = $row[0];
}

return $record;
}
?>

关于php - 输出数据库列中的记录数并将其分配为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com