Wordpress = Site Admin Made Easy

clixcorp.com

February 4th, 2010 at 2:19 am

deciphering arrays/objects/scalars from $wpdb->get_results

in: Queries

As a test run with php regarding a specific $wpdb->get_results query on the wp_terms table, I wrote this to see how the variables would come out in a cleanly formatted way.  You never have any idea until you investigate.  This is similar to using var_export() function of php, and doesn’t have a useful purpose other than letting you get a look-see inside the returned array, with a better explanations of each key->value (etc) than var_export() provides.  (To this day I still look at var_export() results sometimes and scratch my head).

Supporting up to two levels of complexity, hopefully it can help you around some of the dark alley’s of phpism ala WordPress.

Something like this:

function my_test_results(){
 global $wpdb;
 $table_name = $wpdb->prefix . 'terms';
 $query="select term_id, name, slug from $table_name order by term_id asc";
 $y=$wpdb->get_results($query,OBJECT);  // I specified object for this test.  ARRAY_A will test out differently.
 $x=clix_array_decode($y);
 update_option('my_sql_results',$x);  // Use phpmyadmin to view the wp_options table results.
}
/* ************************************************************
function clix_array_decode($input){
 if(is_object($input)){
     $array=(array)$input;
    $text="Was an object on input\n\n";
    }
 elseif(!is_array($input)){
    $array = (array) $input;
    $text="Was not array on input\n\n";
     }
 else{
     $array=$input;
     $text="Was an array on input\n\n";
     }
 if(is_array($array)){
     while(list($key,$value)=each($array)){
         if(is_string($key) && is_string($value)){
             $text.="$key => $value\n";
             }
         elseif(is_string($key) && is_array($value)){
             for ($i=0; $i<count($value); $i += 1) {
                  if(is_array($value[$i])){
                  $text.="$key = ARRAY\n";
                  }
         else{
              $text.="$key => $value[$i]\n";
             }
        }
     }
 elseif(is_string($key) && is_bool($value)){
     if(is_bool($value)===TRUE){
         $text.="$key => TRUE\n";
         }
     else{
         $text.="$key => FALSE\n";
         }
     }
 elseif(is_string($key) && is_null($value)){
     $text.="$key => NULL\n";
     }
 elseif(is_array($key)||is_array($value)||is_object($value)){
     if(is_array($key)){
         $text.="2 key is array\n";
         }
     elseif(is_object($key)){
         $text.="2 key is object\n";
         }
     elseif(is_scalar($key)){
        $text.="2 key is scalar - key:$key\n";
         }
     if(is_array($value)){
        $text.="2 value is array\n";
         }
     if(is_object($value)){
        $text.="2 value is an object\n";
        }
     if(is_scalar($value)){
        $text.="2 value is scalar - key:$key\n";
        }
     $text.="\n";
        while(list($key1,$value1)=each($value)){
             if(is_string($key1) && is_string($value1)){
                 $text.="  $key1 => $value1\n";
                 }
            elseif(is_string($key1) && is_array($value1)){
                 for ($i=0; $i<count($value1); $i += 1) {
                     if(is_array($value1[$i])){
                         $text.="  $key1 = ARRAY\n";
                         }
                     else{
                          $text.="  $key1 => $value1[$i]\n";
                         }
                     }
                }
            elseif(is_string($key1) && is_bool($value1)){
                 if(is_bool($value1)===TRUE){
                      $text.="  $key1 => TRUE\n";
                      }
                 else{
                      $text.="  $key1 => FALSE\n";
                      }
                  }
            elseif(is_string($key1) && is_null($value1)){
                $text.="  $key1 => NULL\n";
                }
            elseif(is_array($key1)||is_array($value1)){
                $text.="  key/value => Array\n";
                }
            else{
                $text.="  Unknown Type1!\n";
                }
            }
        }
        else{
           $text.="Unknown Type!\n";
        }
    }
 }
 return $text;
 }

With the returned value $text, all you need to do is update_option(‘my_sql_results’, $text) and then you can view in the wp_options table what is going on with the data; (phpmyadmin).

I know there are probably self->recursive methods out there to do the same thing but I was a bit lazy tonight. Going one level down manually was easier than figuring out multiple levels/dimensions of the returned object/array, which is what var_export() always does.


-

 

RSS feed for comments on this post | TrackBack URI

Query Count: 37 queries. 0.182 seconds.