This is my code

global $wpdb;
$rows = $wpdb->get_results("SELECT `submit_time` AS 'Submitted', max( if( `field_name` = 'Salutation', `field_value` , NULL ) ) AS 'Salutation', max( if( `field_name` = 'First Name', `field_value` , NULL ) ) AS 'First Name', max( if( `field_name` = 'Last Name', `field_value` , NULL ) ) AS 'Last Name', max( if( `field_name` = 'Title', `field_value` , NULL ) ) AS 'Title'
FROM `wp_cf7dbplugin_submits`
WHERE `form_name` = 'Sign Up'
GROUP BY `submit_time`
ORDER BY `submit_time` DESC
LIMIT 0 , 100

foreach($rows as $a){
echo $a->field_value;//field value is col name

What I am doing wrong here ?

  • tried $wpdb->get_var() and it worked , thanks btw. Jul 8, 2015 at 13:39
  • I am glad you figured it out. :) Sometimes all it takes is taking a step away and coming back to it to figure it out.
    – Nicole
    Jul 8, 2015 at 13:41
  • @PieterGoosen just did :) Jul 9, 2015 at 15:26
  • Just remember to come back and accept it as soon as the restriction is lifted :-) Jul 9, 2015 at 16:24

2 Answers 2


After struggling with different functions , I figure out $wpdb->get_row() and $wpdb->get_var()
The first one returns rows into an associative array or numerical array , depends on the second argument in $wpdb->get_row('query',ARRAY_A or ARRAY_N or OBJEECT) and the second one that I used to solve my problem return only one variable from particular table.

$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
echo "<p>User count is {$user_count}</p>";

Reference link


Basically what is the difference btwn $wpdb->get_results and $wpdb->get_var, I see your query is working fine after removing where clause.

Just know that one thing $wpdb->get_results returns an array.

  • $wpdb->get_results() is more generic $myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );, i think and it returns rows in an associative array while $wpdb->get_var() is specific to get one variable from a table like <?php $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" ); echo "<p>User count is {$user_count}</p>"; ?> Reference link Jul 8, 2015 at 18:14
  • Yeah... that's true, then did you find, what was the reason,get_results() is not working with your query, even we have rows in our result-set. Jul 9, 2015 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.