user3224207
user3224207

Reputation: 437

CakePHP Mysql query with AND condition

I am running a Mysql query in CakePHP but it is not outputting any result though no error is displayed.

Mysql query is this:

SELECT `isbn`, `title`, `author_name` FROM `books` WHERE `author_name` = 'William Rice' AND `title` LIKE '%Course%' ORDER BY `title` ASC

and it outputs results.

And I have written this in CakePHP find function:

$books = $this->Book->find('all', array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
                                                'conditions'=>array('Book.author_name'=>'William Rice', 
                                                                    'AND'=>array('Book.title'=>'LIKE %Course%')),
                                                'order'=>'Book.title ASC',
                                                  ));

But the query doesn't run. Is AND condition specified like this? Please have a look at it and tell me where is it wrong. Thanks.

Upvotes: 0

Views: 302

Answers (3)

Urdesh Kumar
Urdesh Kumar

Reputation: 1140

You have done only one mistake , Remove 'AND' from your query, after removing 'AND' your query will be like below query.

$books = $this->Book->find('all', array(
        'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
        'conditions' => array(
            'Book.author_name'=>'William Rice',
            'Book.title LIKE'=>'%Course%'
            ),
        'order'=>'Book.title ASC',
        )
    );

Upvotes: 0

Anubhav
Anubhav

Reputation: 1623

Do not use AND array:

You have to change your query as given below:

$books = $this->Book->find('all', 
array('fields'=>array('Book.isbn', 'Book.title', 'Book.author_name'),
      'conditions'=>array('Book.author_name'=>'William Rice',
                          'Book.title LIKE'=>'%Course%'),
      'order'=>'Book.title ASC'
      ));

Upvotes: 1

cornelb
cornelb

Reputation: 6066

    $books = $this->Book->find('all', array(
        'fields' => array('Book.isbn', 'Book.title', 'Book.author_name'),
        'conditions' => array(
            'Book.author_name'=>'William Rice',
            'Book.title LIKE'=>'%Course%'
        ),
        'order'=>'Book.title ASC',
    ));

Upvotes: 0

Related Questions