I create this table "plan"

```
======================
| id | name | status |
| 1  | Alpha|   1    |
| 2  | Beta |   1    |
| 3  | Char |   1    |
======================

```

And this table as sub of plans "plans\_sub"

```
===================================
| id | name | min | max | main_id |
| 1  | v1   |  1  | 10  |    2    |
| 2  | v2   | 11  | 20  |    2    |
| 3  | v3   | 21  | 100 |    2    |
===================================

```

I created a form which is contains input type number. After submitted the form It will send a "plan\_id" and "amount" data.

I want to get the database select based on the form submitted data. For example the form send me plan\_id = 2 and amount = 11 then I created this PDO.

```
$stmt = $conn->prepare("SELECT * FROM plans p INNER JOIN plans_sub ps ON ps.main_id = p.id WHERE ps.main_id = :pid AND (min >= :amount OR min IS NULL) AND (max <= :amountOne OR max IS NULL)");
$stmt->bindValue(':pid',$_POST['plan_id']);
$stmt->bindValue(':amount',$_POST['amount']);
$stmt->bindValue(':amountOne',$_POST['amount']);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$planId = $stmt->fetch();

```

When I try to get the result by print it.

```
print_r($planId);

```

It gave me no data. It should give me data of

```
Plan: Beta
plan sub: v2

```

because of the submited data is plan id 2 (main\_id in plans\_sub) and amount 11.

=========================================

EDIT
====

I try to use foreach like this 

```
$stmt = $conn->prepare("SELECT * FROM plans p INNER JOIN plans_sub ps ON ps.main_id = p.id WHERE main_id = :pid");
$stmt->bindValue(':pid',$_POST['plan_id']);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$plans = $stmt->fetchAll();
foreach ($plans as $key => $value) {
 if($_POST['amount']>=$value['min'] && $_POST['amount']<=$value['max']){
     $planid = $value;
 }
}

```

and print\_r($planid ) to see the result. Yes it give me **Beta v2** with submitted form amount = 11 and plan\_id = 2.

But I have next questions. How if the submitted form is amount = 0 and plan\_id = 2 ?? If the submitted form is like that I want to show the result as a **Beta v1** with amount change to 1 as the minimal min in the "plans\_sub".

Can anyone help me to fetch the data?