2017-05-30 56 views
0
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如何使用數組值來查詢的MySQL/MariaDB的

//這裏是我的查詢第一個查詢 $ 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找DATAS其中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 

我想要實現這個什麼是我

+0

查看我添加的新標籤。 –

回答

0

因爲您的覆蓋變量,所以它將只存儲最後一個值,即3

將此行更改爲此。存儲所有的ID陣列

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

在一個查詢,其中這樣的子句中使用IN。使用implode在IN子句中傳遞all id這樣的子句。

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

感謝您的迴應我試過你的方法,但我得到的錯誤是:致命錯誤:[]運算符不支持字符串在C:\ xampp \ htdocs \ mlmpro \ mem \ matrix.php上線115 – ebenezer

+0

您需要初始化$ id是這樣的數組$ id = array(); while($ r = $ stmt-> fetch(PDO :: FETCH_ASSOC)){...} @ebenezer – JYoThI

+0

我也試過,但我得到了另一個錯誤警告:implode():無效參數傳遞在C:\ xampp \ htdocs \ mlmpro \ mem \ matrix.php在線125我發現的是,當我回顯ID它顯示23沒有任何逗號像2,3所以我試過這種方法$ id = array(); \t \t \t \t \t \t \t \t \t而($ R = $ stmt->取(PDO :: FETCH_ASSOC)){ \t \t \t \t \t \t \t \t \t \t \t \t $ ID [] = $ R [ 'mem_id']; \t \t \t \t \t \t \t \t但它只能隨聲附和arrayarray當我試圖呼應ID,但能不能,因爲我使用MariaDB的 – ebenezer

相關問題