Title: SQL doesn&#8217;t return values
Last modified: August 19, 2016

---

# SQL doesn’t return values

 *  Resolved [yyeric](https://wordpress.org/support/users/yyeric/)
 * (@yyeric)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/sql-doesnt-return-values/)
 * I have this below SQL which looks at NextGen Gallery’s wp_ngg_tags table, and
   do a count. Then it loops through all the rows to bring back the name value.
 * I couldn’t get it to do what I wanted, but it does the “count” alright.
 *     ```
       <?php
   
       	global $wpdb;
       	$sql = 'SELECT COUNT(*) FROM wp_ngg_tags ';
       	$count= $wpdb->get_var($sql);
       echo $count;
       echo "<hr>";
   
       for ( $counter = 1; $counter <= $count; ) {
   
       $row = 'SELECT name FROM wp_ngg_tags where id='.$counter;
   
       $name= $wpdb->get_var($row);
   
       echo $name;
   
       $counter=$counter+1;
   
       }
       ?>
       ```
   
 * Any help would be appreciated.

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

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 7 months ago](https://wordpress.org/support/topic/sql-doesnt-return-values/#post-880429)
 * Why in the world would you do it that way? Most strange way of doing something
   that I’ve ever seen…
 * If you want to loop through all the names, then don’t select them one at a time.
   That’s pointless and way, way strange.
 *     ```
       $results = $wpdb->get_results('SELECT name FROM wp_ngg_tags');
       foreach ($results as $row) {
       echo $row->name;
       }
       ```
   
 * Simple.
 *  Thread Starter [yyeric](https://wordpress.org/support/users/yyeric/)
 * (@yyeric)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/sql-doesnt-return-values/#post-880449)
 * That is just brilliant, I will need to find more doco to work on my SQL for WP.
 * Thanks Otto
 * Eric

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

The topic ‘SQL doesn’t return values’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [yyeric](https://wordpress.org/support/users/yyeric/)
 * Last activity: [17 years, 7 months ago](https://wordpress.org/support/topic/sql-doesnt-return-values/#post-880449)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
