[support] String conversion

Greg Knaddison greg at growingventuresolutions.com
Mon Apr 11 02:45:49 UTC 2011


On Sun, Apr 10, 2011 at 8:35 PM, Austin Einter <austin.einter at gmail.com> wrote:
> Hi All
> On further exploring..
>
>  $skill = "PHP, TML, HTML";
>  $pieces = explode(",", $skill);
>
> $query_string = 'SELECT name, email, phone, yrsofexp FROM {resubmt_table} '
> .
>   'WHERE resubmt_table.uid IN (SELECT DISTINCT uid FROM
> {resubmt_users_skills} ' .
>   'WHERE resubmt_users_skills.skillid IN(SELECT skillid FROM
> {resubmt_skills} ' .
>   'WHERE resubmt_skills.skillname IN ( ' . db_placeholders($pieces,
> 'varchar') . ')))';
>
>  drupal_set_message($query_string);
>  $query = db_query($query_string);
>
>
> The output prints as
>
> SELECT name, email, phone, yrsofexp FROM {resubmt_table} WHERE
> resubmt_table.uid IN (SELECT DISTINCT uid FROM {resubmt_users_skills} WHERE
> resubmt_users_skills.skillid IN(SELECT skillid FROM {resubmt_skills} WHERE
> resubmt_skills.skillname IN ( '%s','%s','%s')))
>
> But I do not see , pieces[0], pieces[1] and pieces[2] etc in query string.
> Is it fine or I am doing something wrong here.
>

You are close, but you need one last piece:

$query_string = 'SELECT name, email, phone, yrsofexp FROM {resubmt_table} ' .
  'WHERE resubmt_table.uid IN (SELECT DISTINCT uid FROM
{resubmt_users_skills} ' .
  'WHERE resubmt_users_skills.skillid IN(SELECT skillid FROM
{resubmt_skills} ' .
  'WHERE resubmt_skills.skillname IN ( ' . db_placeholders($pieces,
'varchar') . ')))';

 drupal_set_message($query_string);
 $query = db_query($query_string, $pieces);
                                         ^^^^^^^^^^^^^^^^^^

The $pieces as an argument to $db_query should get it done.

Also, for debugging sql I love using the Devel module and enabling
query logging and printing the query log. It shows the exact query
without having to do a drupal_set_message and after any argument
substitution takes place.

Cheers,
Greg

-- 
Greg Knaddison | 720-310-5623 | http://growingventuresolutions.com
http://masteringdrupal.com - Videos and Tutorials


More information about the support mailing list