Skip to:
Content
Pages
Categories
Search
Top
Bottom

\"$wpdb->prepare\" and LIKE \'%….%\'

  • Avatar of Grosbouff
    grosbouff
    Participant

    @grosbouff

    Hi, I have a problem with a query in my component.

    a part of my code for the query :

    if ($more_sql)

    $query_where = $usermeta_sql.’WHERE ‘.implode(‘ AND ‘,$more_sql);

    $sql = $wpdb->prepare(

    “SELECT DISTINCT {$fields_to_get}

    FROM {$bp->classifieds->table_name} c

    INNER JOIN {$bp->classifieds->table_name_classifiedmeta} cm ON cm.classified_id = c.id

    {$query_where}

    {$group_by}

    {$sort_by}

    {$order}

    {$pag_sql}

    );

    The $more_sql is an array that contains the statements for the query.

    For example;

    if ($user_id) {

    $more_sql['creator_id'] = $wpdb->prepare(“c.creator_id = %d”,$user_id);

    }

    The problem is that for one of the statements, it doesn’t work. Seems it because of the % chars.

    How can I fix this ?

    if ($group)

    $more_sql['group'] = $wpdb->prepare(“cm.meta_key = ‘groups’ AND cm.meta_value LIKE ‘%s'”,’%”‘.$group.'”%’);

    Thanks guys !

Viewing 3 replies - 1 through 3 (of 3 total)
  • Avatar of John James Jacoby
    John James Jacoby
    Keymaster

    @johnjamesjacoby

    Try…

    $more_sql['group'] = $wpdb->prepare("cm.meta_key = 'groups' AND cm.meta_value LIKE '%%$group%%'");

    Avatar of Grosbouff
    grosbouff
    Participant

    @grosbouff

    (I need the quotes around $group, so :)

    $more_sql['group'] = $wpdb->prepare(“cm.meta_key = ‘groups’ AND cm.meta_value LIKE ‘%%”$group”%%'”);

    print_r($more_sql['group']);

    returns

    cm.meta_key = ‘groups’ AND cm.meta_value LIKE ‘%”8″%

    as it should (thanks !)

    then

    print_r(“SELECT DISTINCT {$fields_to_get}

    FROM {$bp->classifieds->table_name} c

    INNER JOIN {$bp->classifieds->table_name_classifiedmeta} cm ON cm.classified_id = c.id

    {$query_where}

    {$group_by}

    {$sort_by}

    {$order}

    {$pag_sql}

    “);

    works but not with prepare :

    print_r($wpdb->prepare( “SELECT DISTINCT {$fields_to_get}

    FROM {$bp->classifieds->table_name} c

    INNER JOIN {$bp->classifieds->table_name_classifiedmeta} cm ON cm.classified_id = c.id

    {$query_where}

    {$group_by}

    {$sort_by}

    {$order}

    {$pag_sql}

    “));

    which returns nothing… :( :( :(

    help! :)

    Avatar of Grosbouff
    grosbouff
    Participant

    @grosbouff

    Ok, I finally get it by using $wpdb->prepare when my sql query is built and not before (when I build the arguments)…

    Thanks for the help !

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘\"$wpdb->prepare\" and LIKE \'%….%\'’ is closed to new replies.