Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to run a database query as below:

$id = $_GET['id'];
$query = 'select * from ';
$query .= $wpdb->get_blog_prefix() . 'fxdescription ';
$query .= 'where id= '.$id;
$currency = $wpdb->get_results( $wpdb->prepare( $query ), ARRAY_A );

The result of doing print_r($currency) is as below:

Array ( 
[0] => Array ( [id] => 1 [code] => EUR [name] => Euro [description] => [format] => direct ) 
)

The question is - is the above result normal, as the result is an array inside an array, and to access the data I have to use $currency['0']['code'], in place of $currency['code']?

share|improve this question
    
Side note: I do like this echo '<pre>' . print_r( $currency, true) . '</pre>' to get an easier to read output. –  brasofilo Nov 18 '12 at 11:32
add comment

2 Answers

  1. its normal for get_results method
  2. you can use get_row method to get only one row.
share|improve this answer
    
Thanks for the help. Tried get_row method, and it worked perfectly. –  Deepak Kundu Nov 18 '12 at 11:27
add comment

No, the default output type of get_results is object, but you've set it to associative arrays.

Yes, you'll need to use $currency[0]['code'], or you could use

foreach ( $currency as $c ) {
    echo $c['code']; // can now be accessed
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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