0

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.

3 Answers 3

1

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'
      ));
Sign up to request clarification or add additional context in comments.

1 Comment

Anubhav thanks but I already tried what you suggested. LIKE will be appended to Book.tite
0
    $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',
    ));

Comments

0

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',
        )
    );

1 Comment

Thank you urdesh, but even with that AND the query runs fine. The only problem is LIKE should be with fieldname not with value. Such use of AND is valid but redundant in this case because it defaults to AND. For OR condition it is done that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.