• Resolved aikorei

    (@aikorei)


    I’m having an issue where a MySQL query works in one spot, but not another with (apparently) the same syntax.

    The first query (that works) is:

    $k_user_id = $current_user->ID;
    $k_table = $wpdb->prefix . 'bp_friends';
    $k_friends1 = $wpdb->get_col( "SELECT initiator_user_id FROM $k_table WHERE friend_user_id = $k_user_id AND is_confirmed = 1" );

    The second query (that doesn’t work) is:

    $app_table = $wpdb->prefix . 'app_appointments';
    $sql = $wpdb->prepare('SELECT COUNT(worker) FROM $app_table WHERE worker = $user_id AND status = "completed" ');
    $app_count = $wpdb->query($sql);

    The error that the second query produces is:

    WordPress database error Table 'my_table.$app_table' doesn't exist for query

    Why is the variable not passing through the actual table name? The query is grabbing the variable name (i.e. $app_table), and not the contents of the variable (i.e. app_appointments).

    Any ideas what I’m doing wrong here?

  • The topic ‘MySQL syntax error?’ is closed to new replies.