1

I am using the Contact Form 7 Plugin to keep data of a form and I would like to query a table to show the results in a template. So this is what my query is:

global $wpdb;

$query = "SELECT count(*) as num 
            FROM wp_cf7dbplugin_submits a,wp_cf7dbplugin_submits b
            WHERE a.field_name = 'Buyer'
                AND a.field_value = 1 
                AND a.submit_time = b.submit_time 
                AND b.field_name = carbrand
                AND b.field_value='Mercedes'";

$results = $wpdb->get_row( $query, OBJECT );
var_dump( $results );

For some reason var_dump() spits out a NULL value. When i run the same query in mysql console I see the result that it is supposed to return 2.

Can anyone advise me on what I'm doing wrong? I went over The Codex to integrate $wpdb variable but I can't say I was successful...

1
  • Do you have debugging on? Maybe start with a simpler more basic query and work your way up to that point to find out where it specifically "breaks" ( returns null ). Also, note that questions regarding 3rd party plugins ( such as Contact Form 7 ) are considered to be Off-Topic here.
    – Howdy_McGee
    Aug 23, 2016 at 16:24

1 Answer 1

0

Try this.

$query = "SELECT * FROM wp_cf7dbplugin_submits a,wp_cf7dbplugin_submits b  WHERE a.field_name = 'Buyer' AND a.field_value = 1 AND a.submit_time = b.submit_time AND b.field_name = carbrand AND b.field_value='Mercedes'";

global $wpdb;
// return the number of rows found
$results = $wpdb->num_rows($query);
var_dump($results);
4
  • Unfortunately, it didn't work.
    – Mr.T
    Aug 23, 2016 at 16:32
  • what it is printing in $results
    – Aftab
    Aug 23, 2016 at 16:37
  • there is no output, i guess it is NULL
    – Mr.T
    Aug 23, 2016 at 16:39
  • there is no output, i guess it is NULL, when i change $wpdb->num_rows($query) to $wpdb->get_col($query) or $wpdb->num_results($query) , i see array(0) { }
    – Mr.T
    Aug 23, 2016 at 16:47

Your Answer

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

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