Forums

[resolved] SQL doesn't return values (3 posts)

  1. yyeric
    Member
    Posted 3 years ago #

    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.

  2. Otto
    Tech Ninja
    Posted 3 years ago #

    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.

  3. yyeric
    Member
    Posted 3 years ago #

    That is just brilliant, I will need to find more doco to work on my SQL for WP.

    Thanks Otto

    Eric

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags