1

I have written a function which takes a database and a string and finds a match in the database and returns the corresponding values of book ISBNS..However it's not working for me....I think the problem is when I am matching the names....so for example I have Jack Thorne....When I pass it to the function am I supposed to put a comma in between since am getting the first name Jack from the first name column and the last name from the last name column....I have tried deliberately passing it a name of an author I know exists but it's not returning anything..Please help Here is the function

function booksByAuthor($db,$author){
    $query='SELECT isbn FROM books_authors WHERE author_id = (SELECT author_id FROM authors WHERE first_name,last_name = :author)';
    $statement = $db->prepare($query); 
    $statement->bindValue(':author', $author);
    $statement->execute(); 
    $isbns= $statement->fetchAll();  
    $statement->closeCursor();  
    return $isbns; 
}
3
  • WHERE x,y=z is a meaningless statement. You mean WHERE x=z OR y=z presumably. Commented Nov 4, 2016 at 2:23
  • Possible duplicate of Combine two columns in SQL for WHERE clause Commented Nov 4, 2016 at 2:26
  • 2
    I'm not sure this is the best schema. A lot of authors don't have both first and last names, plus splitting the name up is often non-trivial. How do you split Cyrano de Bergerac? Arthur C. Clarke? Plato? Commented Nov 4, 2016 at 2:26

1 Answer 1

1

Try this:

$query='SELECT isbn FROM books_authors 
WHERE author_id IN 
(SELECT author_id FROM authors WHERE first_name =:author OR last_name = :author)';

the equals is changed to IN - since the SELECT author_id ... statement will create several results.

the first_name =:author OR last_name = :author - to get results that will look into both first_name or last_name column.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion, that actually pointed me into the right direction, since I was passing the name as one string I decided to explode it and then use your querry..It worked...

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.