SQL Query giving an error

42 views
Skip to first unread message

Akriti Anand

unread,
Sep 14, 2017, 6:34:53 AM9/14/17
to Joomla! General Development
So I am trying to query the database to give me the title from module table.
$var = $data['title'];
$db = $this->getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName('title'))
->from($db->quoteName('#__modules'))
->where($db->quoteName('title'). ' LIKE '. $var;
$db->setQuery($query);
$db->execute();

Where $var is the module title that is present in the title column of module table.

But I am getting this error: Save failed with the following error: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION joomla.<title> does not exist.

<title> exists in the title field. Why is it treating like some function? I don't understand where I am going wrong.

Help would be appreciated. Thank you :)

Akriti Anand

unread,
Sep 14, 2017, 6:44:23 AM9/14/17
to Joomla! General Development
So I was giving <title> as Search (1) which it was considering as a function. Now I removed (1) to make $var = 'Breadcrumbs'. It now throws error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Breadcrumbs' in 'where clause'.

It seems I am still stuck :/

Viper

unread,
Sep 14, 2017, 9:01:15 AM9/14/17
to Joomla! General Development
Escape $var with single quote.

->where($db->quoteName('title') . " LIKE '" . $var . "'");

SniperSister

unread,
Sep 15, 2017, 6:50:01 AM9/15/17
to Joomla! General Development
Better use $db->quote($var) as quote by default also escapes the value which prevents SQLi attacks if $var is user-supplied input.

Todor Iliev

unread,
Sep 15, 2017, 3:13:35 PM9/15/17
to Joomla! General Development
You have to escape and put $var in quotes with $db->quote();
Here you are an example.

$db    = $this->getDbo();

$escaped = $db->escape($data['title'], true);
$quoted = $db->quote('%' . $escaped . '%', false);

$query = $db->getQuery(true);
$query
    ->select('a.title')
->from($db->quoteName('#__modules', 'a'))
->where('a.title LIKE '. $quoted);

$db->setQuery($query);
$db->execute();

Akriti Anand

unread,
Sep 16, 2017, 6:12:23 AM9/16/17
to Joomla! General Development

So I followed this. Now there is a new error: Save failed with the following error: 00000, ,

Viper

unread,
Sep 16, 2017, 6:34:24 AM9/16/17
to Joomla! General Development
echo $query;
Reply all
Reply to author
Forward
0 new messages