Title: MySQL syntax error?
Last modified: August 20, 2016

---

# MySQL syntax error?

 *  Resolved [aikorei](https://wordpress.org/support/users/aikorei/)
 * (@aikorei)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/mysql-syntax-error/)
 * 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.

## Tags

 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [aikorei](https://wordpress.org/support/users/aikorei/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/mysql-syntax-error/)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
