Title: sql not working
Last modified: August 31, 2016

---

# sql not working

 *  [ukndoit](https://wordpress.org/support/users/ukndoit/)
 * (@ukndoit)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/sql-not-working/)
 * I created a few tables in my main wordpress database…
 * table names:
 * cards
    scards
 * I put a plugin that lets me use php in pages, I use it in my other wordpress 
   sites, very succesfully… but in this new website I’m working on, I cannot get
   the SQL to work right.
 * Here is what I am trying to do:
 *     ```
       [php] // This opens a PHP Tag like <?php would...
   
       global $wpdb;
   
       $_openm = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM cards" ) );
       $_amtClosed = $wpdb->get_var( $wpdb->prepare( "SUM(f1) from scards" ) );
   
       [/php]
   
       Content... content... content... then I use the variables...
       Open cards ([php] echo $_openm; [/php])
       Closed cards total funds ([php] echo $_amtClosed; [/php])
       more content...
       ```
   
 * But the variables are blank on the page output.
 * I have tested the php tag like this:
 * `[php] echo "Hello newbie";[/php]`
    and that does output that, so it is executing
   the php.
 * Can you tell me what I did wrong? I even tried to put those variables as global
   in case it was not remembering them after I closed the first php tag…
 * still nothing.

Viewing 1 replies (of 1 total)

 *  Thread Starter [ukndoit](https://wordpress.org/support/users/ukndoit/)
 * (@ukndoit)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/sql-not-working/#post-6979742)
 * I see my crucial mistake in this:
 * `$_amtClosed = $wpdb->get_var( $wpdb->prepare( "SUM(f1) from scards" ) );`
 * particularly this one:
    `"SUM(f1) from scards"`
 * I forgot the SELECT part of that statement… I corrected that, but the code is
   still not working.

Viewing 1 replies (of 1 total)

The topic ‘sql not working’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [ukndoit](https://wordpress.org/support/users/ukndoit/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/sql-not-working/#post-6979742)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
