如何使用数组值来查询mysql / mariadb

时间:2017-05-30 03:41:44

标签: php mysql mariadb hierarchical-data

I have a mysql table named tbl_member with the following datas
--------------------------------------------------------
mem_id      fullname    username    refrl   
--------------------------------------------------------
1       testing     jerry01     0   
--------------------------------------------------------    
2       ebenezer    ebenezery4  1
---------------------------------------------------------
3       aminat      aminat      1
---------------------------------------------------------
4       olaniyi     ebobofinger 2
---------------------------------------------------------
5       restoration restor      2
---------------------------------------------------------
6       adeyinka    cmaster     3
---------------------------------------------------------
7       aledo       akat02      3
---------------------------------------------------------   
8       friday      frayo       4
---------------------------------------------------------
9       ajoke       ajoks       4
---------------------------------------------------------
10      femi        lordfex     5   
---------------------------------------------------------

我想要的第一个查询是从tbl_member获取mem_id,其中refrl为1,这意味着我想获取jerry01引用的成员列表

//这是我的查询第一个查询        $ bestid = 1;

    $query_frst = "SELECT * FROM tbl_member WHERE refrl = :sess_id";
    $stmt = $DB->prepare($query_frst);
    $stmt->bindValue(":sess_id", $bestid);
    $stmt->execute();                                                   
    $r = $stmt->rowCount();
    if($r>0){
    while ($r = $stmt->fetch(PDO::FETCH_ASSOC)){

                    $id = $r['mem_id'];             
                    //$N = count($id);
                    //for($i=0; $i < $N; $i++);
                    //echo $N;
                    echo $id;

                                }
            }

$ id将返回值2,3 那是ebenezery4和aminat由jerry01引用

which are:
--------------------------------------------------------
mem_id      fullname    username    refrl   
--------------------------------------------------------    
2          ebenezer    ebenezery4   1
---------------------------------------------------------
3         aminat        aminat      1
---------------------------------------------------------

现在我想要做的是我想查询我的tbl_member以查找refrl为2,3的数据这里是我使用的查询

    $queryy = "SELECT * FROM tbl_member WHERE refrl = :id";
    $stmt = $DB->prepare($queryy);
    $stmt->bindValue(":sess_id", $id);
    $stmt->execute();                                                   
    $remi = $stmt->rowCount();
    if($remi>0){
            echo"<table class='table table-bordered table-hover table-striped tablesorter'>";
            echo"<thead>";
                echo"<tr>";
                echo"<th>Username</th>";
                echo"<th>Fullname</th>";
                echo"<th>Phone number</th>";
                echo"<th>Current Stage</th>";
                echo"<th>Level</th>";
                echo"</tr>";
                echo"</thead>";
                echo"<tbody>";
                   while ($row1 = $stmt->fetch(PDO::FETCH_ASSOC)){
                            extract($row1);
                 echo"<tr>"; 
                    echo"<td>{$username}</td>";
                    echo"<td>{$fullname}</td>";
                    echo"<td>{$phoneno}</td>";
                    echo"<td>{$stage}</td>";
                    echo"<td>{$level}</td>";
                    echo"</tr>";
                    echo"</tbody>";
                    }
                    echo"</table>";
                    }else{
        echo "<div class='alert alert-danger alert-dismissible fade in' role='alert' >You do not have any purchased product.</div>";
                        } 


But my result return only the data of 3 which are the member referred by aminat alone

--------------------------------------------------------
mem_id      fullname    username    refrl   
--------------------------------------------------------
6       adeyinka    cmaster     3
---------------------------------------------------------
7       aledo       akat02      3
---------------------------------------------------------

instead of the values of 23 which should be


--------------------------------------------------------
mem_id      fullname    username    refrl   
--------------------------------------------------------
4       olaniyi     ebobofinger 2
---------------------------------------------------------
5       restoration restor      2
---------------------------------------------------------
6       adeyinka    cmaster     3
---------------------------------------------------------
7       aledo       akat02      3
---------------------------------------------------------

i have tried to look for a way to solve it with my little understanding of php but i could not 

我想用此实现的是我

1 个答案:

答案 0 :(得分:0)

因为你因此覆盖变量,所以它只存储最后一个值,即3

将此行更改为此行。将所有id存储在数组

 $id[] = $r['mem_id'];   

在下一个查询中,在where子句中使用IN。使用implode传递IN子句中的所有id就像这样。

    $queryy = "SELECT * FROM tbl_member WHERE refrl In (:id)";
    $stmt = $DB->prepare($queryy);
    $stmt->bindValue(":id", implode(',',$id));
    $stmt->execute();