2

Hi Im a novice in php and Im trying to display a single data from a column of records. I cant find any id/key on the table that could help me to pull out a data using the get_row method, instead Ive used get_results to display all records of that column then ill display it base on thier index number. The problem is it only displaying the word "Array" I been trying to display the index number 4 but I only get error.

$meta_value = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM  wp_gf_entry_meta WHERE entry_id=%d", $entry_id));         
foreach($meta_value as $data){
    $i++;
    if($data[$i]==4){
        echo $data['metavalue'];
    }
 }

Heres the architecture of the array.

169 

Array (

[0] => stdClass Object
    (
        [meta_value] => Patrick
    )

[1] => stdClass Object
    (
        [meta_value] => Mantin
    )

[2] => stdClass Object
    (
        [meta_value] => 069434
    )

[3] => stdClass Object
    (
        [meta_value] => Teacher
    )

[4] => stdClass Object
    (
        [meta_value] => CW Jefferys CI
    )

[5] => stdClass Object
    (
        [meta_value] => 9
    )

[6] => stdClass Object
    (
        [meta_value] => a:1:{s:28:"gravityformsuserregistration";a:1:{i:0;s:1:"1";}}
    )

)

Thank you so much in advance, God Bless!

1
  • 1
    If you want just the one value, isn't there something you could add to the SQL to limit the data retrieved? Commented Apr 28, 2021 at 6:35

1 Answer 1

2

You are getting value as OBJECT and you are accessing as ARRAY. You can use ARRAY_A to get results as ARRAY. Try the below code.

Also key is meta_value and you accessing as metavalue.

$meta_value = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM  wp_gf_entry_meta WHERE entry_id=%d", $entry_id),ARRAY_A);         
foreach( $meta_value as $key => $data ){
    if( $key == 4 ){
        echo $data['meta_value'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.