Retrieve data from MySQL with foreach

I try to retrieve data from my SQL database within an array. It is working as it should but I do have my problems with one specific data entry. Let me explain:

I retrieve my data from a form. If a user fills out the form, the data will be saved in a database and I retrieve the data right now like this:

Read More
$que10 = $wpdb->get_results("SELECT value FROM `wp_rg_lead_detail` WHERE `field_number` = '10'");

Then I am using foreach:

foreach ($que10 as $key8) {
$dd5 = $key8->value;
}

And then I have my array:

$placeholders = array('[id]', '[dd1]', '[dd2]', '[dd3]', '[dd4]', '[dd5]', '[dd6]', '[dd7]', '[dd8]', '[dd9]', '[dd10]', '[dd11]', '[dd12]');

            $actvalues = array($lastgridid, $fullname, $string, $dd3, $dd5, $dd6, $dd7, $dd8, $dd9, $dd10, $dd11, $ga, $dd12);

Please note, that this is just a snippet from my full code but retrieving the data this way works fine.

Problem is now that the form has also a field with six checkboxes. The data will be saved in database like you can see here:

enter image description here

So the field number is 1.1, 1.2, 1.3, 1.4, 1.5 and 1.6! I am trying to retrieve those data with this code:

$que1 = $wpdb->get_results("SELECT value FROM `wp_rg_lead_detail` WHERE `field_number` LIKE '%1.1%' OR `field_number` LIKE '%1.2%' OR `field_number` LIKE '%1.3%' OR `field_number` LIKE '%1.4%' OR `field_number` LIKE '%1.5%' OR `field_number` LIKE '%1.6%'");

It is working but I now use this foreach:

foreach ($que1 as $key12) {
$dd12 = $key12->value;
 }

And as result I only get one checkbox result all the time, even if the user checks four checkboxes, only one checkbox result will be shown.

Can someone tell me how I have to change the foreach code, so that all results will be shown and not only one?

Related posts

Leave a Reply

2 comments

  1. Try this

    $que1 = $wpdb->get_results("SELECT value FROM `wp_rg_lead_detail` WHERE `field_number` LIKE '%1.1%' OR `field_number` LIKE '%1.2%' OR `field_number` LIKE '%1.3%' OR `field_number` LIKE '%1.4%' OR `field_number` LIKE '%1.5%' OR `field_number` LIKE '%1.6%'",ARRAY_A);
    
    while ($row = mysql_fetch_assoc($que10)) {
        echo $row["wp_rg_lead_detail"];
    }
    

    NOTE: YOU will need the last parameter ARRAY_A

  2. Use the mysql IN() function, it is cleaner. If the user checks 4 checkboxes e.g. 1.1, 1.2, 1.5, 1.6 the below will output four rows.

    $q = "SELECT value 
          FROM `wp_rg_lead_detail`
          WHERE `field_number` IN ('1.1', '1.2', '1.3', '1.4', '1.5', '1.6') ";
    
    $r = $wpdb->get_results($q);
    
    foreach ($que1 as $key12) {
        echo $key12->value . "<br>";
    }