# SQL WHERE problem

**URL:** <https://discourse.cakephp.org/t/sql-where-problem/1007>\
**Category:** Need Help\
**Created:** [August 19, 2016, 8:50am UTC](https://discourse.cakephp.org/t/sql-where-problem/1007 "2016-08-19T08:50:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirheisenberg](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@sirheisenberg](https://discourse.cakephp.org/u/sirheisenberg)\
**Post date:** [August 19, 2016, 8:50am UTC](https://discourse.cakephp.org/t/sql-where-problem/1007/1 "2016-08-19T08:50:36Z")

</div>

Hi everybody !  
I’m using CakePHP 2 and I have a table named **Sales** and 3 rows named _Seller_, _Buyer_ and _Price_.

In a HTML page, I display the result of this query in a HTML table in myview.ctp :

```
public function myview() {
    $this->set('mysales', $this-Sales>find('all', 
    array('sum(price)AS total','group' => array('seller_name','buyer_name'))));
}

```

In each line where the result is displayed, there’s a link which send the seller name and the buyer name of the line clicked and will help creating a PDF with the sale details :

```
<?php echo $this->Html->link("PDF", array(
    "controller" => "name of controller",
    "action" => "myview_pdf",
    "?" => array(
       "seller_name0" => $thesales['Sales']['seller_name'],
       "buyer_name0" => $thesales['Sales']['buyer_name']
    )
 )); ?>

```

I got this function in the controller which receives the seller and buyer names of the line clicked :

```
public function myview_pdf(){

    $this->set('seller_name1', $this->request->query("seller_name0"));
    $this->set('buyer_name1', $this->request->query("buyer_name0")); 
}

```

Now I want to do this SQL query :

```
SELECT detail1_of_sale, detail2_of_sale, detail3_of_sale, detail4_of_sale
FROM sales
WHERE seller_name = 'seller_name1', buyer_name = 'buyer_name1'

```

Which I hope will help me display the details of the sale involving seller\_name1 and buyer\_name1

What I tried to do is this (in the controller, function myview\_pdf) :

```
$this->set('mysales', $this->Sales->find('all',
            array(
                'fields' => array('detail1_of_sale','detail2_of_sale','detail3_of_sale','detail4_of_sale'),
                'conditions' => array(
                    'seller_name' => $seller_name1, 
                    'AND' => array(
                        array(
                            'buyer_name' => $buyer_name1
                            )))
            )
        ));

```

I don’t know if it’s a syntax mistake or what it just tells me that $seller\_name1 and $buyer\_name1 are undefined variables.

**Note** : When I take off the conditions, the $seller\_name1 and $buyer\_name1 are displayed correctly in myview\_pdf.ctp with a simple echo.

Thanks for your help !

---

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3166_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [August 19, 2016, 9:05am UTC](https://discourse.cakephp.org/t/sql-where-problem/1007/2 "2016-08-19T09:05:53Z")

</div>

Instead of variables $seller\_name1 and $buyer\_name1, use $this-\>request-\>query(“seller\_name0”) and $this-\>request-\>query(“buyer\_name0”) like:

```
'condtitions' => array(
   'seller_name' => $this->request->query("seller_name0"),
   'buyer_name' => $this->request->query("buyer_name0")
),
```

---

<div class="post-metadata">

**Author:** ![sirheisenberg](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@sirheisenberg](https://discourse.cakephp.org/u/sirheisenberg)\
**Post date:** [August 19, 2016, 9:38am UTC](https://discourse.cakephp.org/t/sql-where-problem/1007/3 "2016-08-19T09:38:46Z")

</div>

Thanks for you reply !  
I tried your solution and it seems to work but only the seller name is displayed, I can’t display the buyer name whereas I do the same code for both.
