Search code examples
phpmysqlwordpressfloating-accuracygravity-forms-plugin

query can't find data on wp / gravity forms database


I'm hoping this is an easy question for someone although I am trying to figure it out and getting nowhere. All I am trying to do is have some PHP code query the wordpress database to pull some information out of the table that stores entries for the gravity forms plugin. Here is my code:

global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=1.1") as $key200 => $row200) {
$street_address = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=1.3") as $key200 => $row200) {
$city = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=1.4") as $key200 => $row200) {
$state = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=1.5") as $key200 => $row200) {
$zip_code = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=7") as $key200 => $row200) {
$credit = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id") as $key200 => $row200) {
$form_id = $row200->form_id;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=2") as $key200 => $row200) {
$financing_type = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=3") as $key200 => $row200) {
$home_worth = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=4") as $key200 => $row200) {
$to_borrow = $row200->value;
}

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=6") as $key200 => $row200) {
$down_payment = $row200->value;
 }

foreach( $wpdb->get_results("SELECT * FROM wp_rg_lead_detail WHERE lead_id=$entry_id AND field_number=5") as $key200 => $row200) {
$purchase_price = $row200->value;
}

I am using the $wpdb object to access the wordpress database. Most of these queries are working fine. There are three of them not working (city, state and street address)

I am pretty sure this has something to do with the query select statement having "AND field_number=1.3". The fact that the value it is looking for is 1.3 for some reason is causing a problem. there is a fourth query like this that is working - which is "zip_code" where the field number is 1.5 and it pulls that one in OK...

I know these field numbers are correct (1.1, 1.3 and 1.4) as I can see them in the database that way. So I am thinking I need to somehow modify my queries (at least for those three items) so it can pull in the values properly. I have tried a number of things and nothing seems to help.

So I am hoping someone on here can provide some input on this and what I am doing wrong.

All the best, Gerard


Solution

  • Yikes! Gravity Forms uses FLOAT values for the field_number column.

    WTF? WTF? Don't the Gravity Forms developers know that it's purely accidental when any FLOAT or DOUBLE value comes out exactly equal to another? How can this ever have worked?

    Try this.

    ... AND ROUND(field_number,1) = 1.3
    

    This will convert the floating point as stored to a value that can match.

    It happens that 1.5 comes out clean in floating point, but 1.1 doesn't. Neither does 1.3 or 1.4,