-1

I can't figure out how to properly concatenate my PHP variables in my MySQL query.

$convert_class_id and $convert_char_id are holding numerical values.

When I try to activate my plugin I get an error:

Parse error: syntax error, unexpected '=', expecting ',' or ')'

$skill_select = $wpdb->get_results(
                    $wpdb->prepare("
                SELECT skill_name, char_id, um_id, c.class_id
                FROM `wp_ml_skill_class` sc
                JOIN `wp_ml_skill` s ON (s.skill_id = sc.skill_id)
                JOIN `wp_ml_character` c
                WHERE c.class_id = " . $convert_class_id . " " AND c.char_id = " " . $convert_char_id . "
                "));

Thanks in advance.

1

In SQL statement use placeholders instead of variable: %s (string) or %d (number). The second argument of prepare() is array of variables to substitute into the placeholders.

$skill_select = $wpdb->get_results(
                $wpdb->prepare("
            SELECT skill_name, char_id, um_id, c.class_id
            FROM `wp_ml_skill_class` sc
            JOIN `wp_ml_skill` s ON (s.skill_id = sc.skill_id)
            JOIN `wp_ml_character` c
            WHERE c.class_id = %d AND c.char_id = %d",
            [$convert_class_id, $convert_char_id]));
| improve this answer | |
  • Ok, my query works as intended. Thanks for that. I thought those were SQL wildcards, but I researched what you said and found out that they're PHP string functions. php.net/sprintf – Cody Tetreault Aug 28 '18 at 21:56

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.