I have a query in php that pulls news articles from a database, that, when each WHERE clause is applied independently, works fine.
Example #1
//retrieve all news articles
$sql = 'SELECT post.id, post.headline_en, post.story_en, post.post_stat, post.trans_stat, post.translate, post.published_by,
post.publish_on, system_users.u_username, system_users.u_firstname, system_users.u_lastname
FROM post INNER JOIN system_users ON post.published_by=system_users.u_userid';
// If search terms have been provided...
if($_POST['query'] != '')
{
$sql .= ' WHERE (headline_en LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" OR post.story_en LIKE "%'.str_replace(' ', '%', $_POST['query']).'%")';
}
$sql .= ' ORDER BY post.publish_on DESC';
This seemingly works fine, and limits the results based on the search value provided in $_POST['query'].
Example #1
//retrieve all news articles
$sql = 'SELECT post.id, post.headline_en, post.story_en, post.post_stat, post.trans_stat, post.translate, post.published_by,
post.publish_on, system_users.u_username, system_users.u_firstname, system_users.u_lastname
FROM post INNER JOIN system_users ON post.published_by=system_users.u_userid
WHERE (post.post_stat=3 AND post.trans_stat=3) OR (post.post_stat=3 AND post.translate=0)';
// search option removed
$sql .= ' ORDER BY post.publish_on DESC';
This also works fine, and limits the results to the criteria specified in the WHERE clause.
However, when I try to combine both, it doesn't want to play nice, and I am unsure as to why.
Example #3
//retrieve all news articles
$sql = 'SELECT post.id, post.headline_en, post.story_en, post.post_stat, post.trans_stat, post.translate, post.published_by,
post.publish_on, system_users.u_username, system_users.u_firstname, system_users.u_lastname
FROM post INNER JOIN system_users ON post.published_by=system_users.u_userid
WHERE (post.post_stat=3 AND post.trans_stat=3) OR (post.post_stat=3 AND post.translate=0)';
//If search terms have been provided...
if($_POST['query'] != '')
{
$sql .= ' AND (headline_en LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" OR post.story_en LIKE "%'.str_replace(' ', '%', $_POST['query']).'%")';
}
$sql .= ' ORDER BY post.publish_on DESC';
^^ doesn't work. I've tried various permutations of the above and for the life of me I can't see what's wrong. Any feedback as always would be appreciated.