Using SQL functions in conditionals in Drupal 7 db_select() I am trying to write a condition into a SQL WHERE clause that forces the column comparison to a variable to be compared in lowercase. However the addExpression function isn't accomplishing this (since that is putting the expression in the field selection, not the where clause.

Here is what I have tried:

$category = 'mobile wifi';
$query = db_select('taxonomy_term_data', 'ttd')
    -&gt;fields('ttd', array('tid'));
$query-&gt;innerJoin('taxonomy_vocabulary', 'tv', 'ttd.vid = tv.vid');
$query-&gt;addExpression("LOWER(ttd.name) = $category");
$result = $query-&gt;condition('machine_name', 'images_cat', '=')
    -&gt;execute()
    -&gt;fetchAssoc();


And this:

$category = 'mobile wifi';
$query = db_select('taxonomy_term_data', 'ttd')
    -&gt;fields('ttd', array('tid'));
$query-&gt;innerJoin('taxonomy_vocabulary', 'tv', 'ttd.vid = tv.vid');
$result = $query-&gt;condition('machine_name', 'images_cat', '=')
    -&gt;condition('LOWER(ttd.name)', $category, '=')
    -&gt;execute()
    -&gt;fetchAssoc();


The second one becomes invalid and the first one causes the problem I mentioned above. Any thoughts or suggestions?

Thanks,
Patrick
change

$query-&gt;addExpression("LOWER(ttd.name) = $category");


to

$query-&gt;where('LOWER(ttd.name) = :category', array(':category' =&gt; $category));