I get a “query failed” message when I use below PHP in a web page but if I remove OR column2 LIKE '%$searchterm%' from the query it works fine. I already tried Googling the issue and looked in a for dummies manual. The database table definitely contains the search term in column_2.
/* send this query to MySQL database */
$query = "SELECT * FROM table WHERE column_1 LIKE '%$searchterm%' OR desc LIKE '%$searchterm%'";
/* process query results */
$result = mysqli_query($cxn,$query) or die ("query failed");
$row = mysqli_fetch_assoc($result);
extract($row);
/* create HTML for web broswer to see */
echo "Column 1 value: $column_1<br><br>";
echo "Column 2 value: $column_2<br><br>";
echo "Column 3 value: $column_3<br><br>";
Solution! The query needed backticks around the object names to the left of the
ORstatement while the other object names needed straight-up single-quote marks.I saw the backticks when using phpMyAdmin to translate the query into PHP code but had to ignore the part where backslashes were added as in
\'%$searchterm%\'. So the translation tool led me to a solution but I don’t see a reason for those backslashes.Also, simpler queries with no
ORstatement worked fine with no backticks and minimal quote marks but I don’t see whyORwould invoke the backticks rule.