2

I would like to know how to perform pattern search using JDatabase

Here's my code:

$query->select('Common_name');
$query->from($db->quoteName('common_name'));
$query->where($db->quoteName('Common_name').' LIKE '. $db->quote('\'o%\''));
$db->setQuery($query);
$result = $db->loadResult();
5

try this

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query
  ->select('Common_name')
  ->from($db->qn('your_table_name'))
  ->where($db->qn('Common_name').' LIKE '. $db->quote($db->escape('o%')));

$db->setQuery($query);
$results = $db->loadObjectList(); // use loadResult() for single value result

Documentation is your best friend: https://docs.joomla.org/Accessing_the_database_using_JDatabase

  • it produced an error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE LIKE' at line 3 SQL=SELECT Common_name FROM WHERE LIKE – Joshua Aug 9 '16 at 5:43
  • It works fine to me. I added the $db, $query initialization code also above, please check. – Nagarjun Aug 9 '16 at 5:46
  • actually it still has the same error even if i already initialized the $db and $query – Joshua Aug 9 '16 at 5:56
  • can you post what is the code you write for ->from($db->qn('your_table_name')) – Nagarjun Aug 9 '16 at 6:05
  • ->from($db->qouteName('common_name')) – Joshua Aug 9 '16 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.