如何从mysql数据库中选择主用户及其子用户

时间:2017-06-13 10:55:17

标签: php mysql

我正在开发一个应用程序 - 一种迷你生物指标。在此应用程序中,管理员将创建特定用户并授予他创建其他用户的权限。主用户创建的用户将是为人们创建注册的用户。我有两个表,一个用于管理员用户,另一个用于用户注册。我想要的是使用此子用户添加到users表的用户数来获取admin下的所有子用户。

这里是源代码。它没有给我我需要的东西。

  <div class="content-loader">
    <?php
    if($pri == 'user' && $utype == 'super_admin'){
    ?>

    <table cellspacing="0" width="100%" id="example" class="table display table-striped table-hover table-responsive">
    <thead>
    <tr>
    <th>#ID</th>
    <th>Name</th>
   <th>username</th>
   <th>no of people registered</th>
   <th>Date Added</th>       
    <th></th>

    </tr>
    </thead>
    <tbody>
    <?php


    $i =1;
    $stmt = $db_con->prepare("SELECT * FROM users where adder in (select uid from admin_user where adder = '$id')" );
    $stmt->execute();
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    $ad = $row['adder'];
    $stmt = $db_con->prepare("SELECT * FROM admin_user where uid = '$ad' " );
    $stmt->execute();
    while($rows=$stmt->fetch(PDO::FETCH_ASSOC))
    {

    ?>
        <tr class="delete_user<?php echo $rows['uid']; ?>">
        <td><?php echo $i; $i++;?></td>
        <td><?php echo $rows['name']; ?> </td>
        <td><?php echo $rows['username'];?></td>
       <td><?php //echo $rows['tot'];?></td>
       <td><?php echo $rows['date_added'];?> (<?php echo timeAgo($rows['time_added']) ?>)</td>
      <td class="td-actions text-right">
      <!--<a id="<?php echo $rows['uid']; ?>" class="edit-link btn btn-primary btn-simple btn-xs" rel="tooltip" href="#" title="Edit User"><i class="material-icons">edit</i></a>-->
    <a id="<?php echo $rows['uid']; ?>" class="btn btn-danger btn-user">
        <span class="glyphicon glyphicon-trash"></span>
        </a>
    </td>

        </tr>
        <?php

    }
    ?>
    </tbody>

    </table>
    <?php
    }else{
    ?>  
        <div class="panel-warning" style="padding:7px; text-align:center; font-size:18px;">This page is for the administrator!</div>
        <?php
    }
    ?>

    </div>


                    </div>

为了澄清,这里有两个表: 管理员用户表

enter image description here 管理员用户表Img。 1

管理员及其子用户添加的用户表 enter image description here

用户表Img.2

2 个答案:

答案 0 :(得分:1)

从您的代码中我的建议只执行一个查询。您所需的结果只能在查询中使用。试试这个

$stmt = $db_con->prepare("SELECT * FROM admin_user where uid = (SELECT adder FROM users where adder in (select uid from admin_user where adder = '$id') )" );
$stmt->execute();

答案 1 :(得分:0)

关节很简单

SELECT ad.name as nom_admin, users.society_name as mySociety // I guess society is the name of the user
FROM admin_user ad
LEFT JOIN users ON users.adder= ad.Uid 

如果您想知道每个管理员已注册的用户数

SELECT ad.name as nom_admin, count(users.society_name) as nbSociety //users.IDsociety
FROM users
LEFT JOIN admin_user ad ON ad.Uid = name.adders
GROUP BY cl.classe 

你的桌子听起来很奇怪。安排好田野

我的朋友,我们做的是计算机科学吗? :不,我们做工艺品