get_row returns empty when data exists

I am trying to run the following query to access my WP cf7dbplugin_submits table but it keeps coming up empty. I have added my table into wp-db.php model as such:

Added the contact form table- cf7dbplugin_submits to the following at about line 268:
    var $tables = array('cf7dbplugin_submits','posts', . . .)

At around line 304 add the following:
    /**
     * WordPress cf7dbplugin_submits table
     */
    public $cf7dbplugin_submits;

So the database function should find it (it has when used elsewhere in my code). But the following is consistently coming up empty for any $postid:

function action_getpost($postid) {
  global $wpdb, $out;
  $out['post'] = array();
  $out['uploads'] = array();

    // wp_posts has one row for each submitted form.
    // wp_nf_objectmeta includes a set of rows for each defined form.

    $query =
    "SELECT submit_time, form_name, field_value" .
    "FROM $wpdb-cf7dbplugin_submits " .
    "WHERE field_order = %d AND submit_time = %d ";

    $queryp = $wpdb-prepare($query, array(9999, $postid));
    if (empty($queryp)) {
      $out['errmsg'] = "Internal error: \"$query\" \"$postid\"";
      return;
    }
    $wpdb-show_errors( true );
    $row = $wpdb-get_row($queryp, ARRAY_A);
    if (empty($row)) {
      $out['errmsg'] = "No rows matching: \"$postid\"";
      return;
    }

If I just run the following query in phpMyAdmin, where $postid = 1445125066.4375:

SELECT submit_time, form_name, field_value
FROM wp_cf7dbplugin_submits
WHERE field_order = 9999 AND submit_time = 1445125066.4375 

It produces the needed row

submit_time         form_name       field_value
1445125066.4375     Demographics    usernamehere

So why is the function returning my error of no rows matching 1445125066.4375? Any ideas?

Topic get-row plugin-contact-form-7 wpdb Wordpress

Category Web


As it turns out it was an issue of the decimal point which was pointed by s_ha_dum. But for different reasons. The place holder %d is integer. I need %f for floating point. Now it works. And thanks to s_ha_dum I have more parsimonious code.


You have several things going on there and I may not be able to sort them all out.

  1. Unless $wpdb->cf7dbplugin_submits has been added to the $wpdb object your query won't work. You will need something like {$wpdb->prefix}cf7dbplugin_submits instead but I can't really guess at the right value.
  2. You don't need to swap in 9999. That is a hard-code value. Swapping it in with prepare is a waste of resources.
  3. Prepare accepts 3 different placeholders, one for a string, one for a digit, and one for float. By using %d you are truncating your decimal. You need %f.

Perhaps something like this will work better:

$query =
"SELECT submit_time, form_name, field_value" .
"FROM {$wpdb->prefix}cf7dbplugin_submits " .
"WHERE field_order = 9999 AND submit_time = %f ";

$queryp = $wpdb->prepare($query, $postid);

Item #2 won't cause a failure of the query, but item #1 would, so that is where I am betting this goes wrong. However, item #3 would just as likely break the query as you would not get a match on a decimal value if the decimal input was truncated to an integer.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.