engl vpn : php to write sql for compound query
Trying to cause the search mechanism to include values in the poems.po_pseudonym as well as in the authors.au_surname field. In the file vpn-search/vpn-search.php modified this line:
$conditions[] = $this->db->prepare('LOWER(' . $column .') LIKE %s', '%' . strtolower($val) . '%')
to this :
if ($column == 'authors.au_surname') {
$conditions[] = $this->db->prepare('LOWER(' . $column .') LIKE %s OR LOWER(poems.po_pseudonym) LIKE %s', '%' . strtolower($val) . '%', '%' . strtolower($val) . '%');
}
note that each instance of the %s in the first argument requires a distinct subsequent argument, so that's why the second and third arguments are the same (i.e. '%' . strtolower($val) . '%').
So that gave me a query like this:
SELECT poems.po_id AS poem_id, poems.po_title AS title
FROM poems LEFT JOIN poems_to_authors ON poems_to_authors.pta_po_id = poems.po_id LEFT JOIN authors ON poems_to_authors.pta_au_id = authors.au_id
WHERE length(poems.po_pseudonym) >= 1 AND authors.au_surname LIKE '%Shelley%' OR poems.po_pseudonym LIKE '%Shelley%'
The parsing of the AND and OR operator seems to be in as-they-come order, which means the WHERE clause is effectively:
WHERE (length(poems.po_pseudonym) >= 1 AND authors.au_surname LIKE '%Shelley%') OR (poems.po_pseudonym LIKE '%Shelley%')
but what I want is this:
WHERE (length(poems.po_pseudonym) >= 1) AND (authors.au_surname LIKE '%Shelley%' OR (poems.po_pseudonym LIKE '%Shelley%'
So, I modified another line in that same file to include explicit parentheses, that is from this:
$conditionString = "WHERE " . implode(' AND ' , $conditions);
to this:
$conditionString = "WHERE (" . implode(') AND (' , $conditions) . ')';