簡體   English   中英

如何使用數組值查詢mysql / mariadb

[英]How to use array values to query mysql/mariadb

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   
---------------------------------------------------------

我要我的第一個查詢執行的操作是從refrl為1的tbl_member獲取mem_id,這意味着我想獲取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 

我要達到的目標是

因為您因此覆蓋了變量,所以它將僅存儲最后一個值,即3

將此行更改為此。 將所有ID存儲在數組中

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

在下一個查詢中,在這樣的where子句中使用IN。 使用implode在where子句中傳遞所有id,就像這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM