0

I have been staring at this for the last hour and can't seem to understand why this is not working. The code below gets the info from the table, which is in an array (example below the code).

$post_id = 228;

$content_post = get_post($post_id);
$content = $content_post->post_content;

global $wpdb;

$results =$wpdb->get_results("SELECT * FROM wp_article_hyperlinks"); 

foreach ($results as $result){
// $url = $result["url"];
// $title = $result["title"];

    echo '<pre>';
    var_dump($result["title"]);
    echo '</pre>';
}
}

I am trying to get the title value, but the above just dumps a blank screen. When I run a foreach with a key and value all I get is int or NULL when I use the key.

object(stdClass)#717 (4) {
  ["ID"]=>
  string(1) "1"
  ["time"]=>
  string(19) "2018-08-22 12:30:29"
  ["title"]=>
  string(15) "gluteus maximus"
  ["url"]=>
  string(86) "/wp-content/blahblah"
}

I have compared it to other code/posts on SO and W3Schools and I'm pretty sure that whatever I'm missing is pretty basic but I can't see it.

5
  • 3
    echo $result->title; it's an object not an array. Commented Aug 27, 2018 at 14:55
  • oh thanks, I totally missed that! I will look at the documentation to better understand the differences. Commented Aug 27, 2018 at 14:59
  • 1
    Shouldn't reward code only answers that have absolutely no explanation. Commented Aug 27, 2018 at 15:14
  • 1
    I suppose I felt bad because the answer is correct and they were first - you are right though. The other answer is more comprehensive and its not on a first come first serve basis. got it. Commented Aug 27, 2018 at 15:29
  • not sure why the question was downvoted, it would be great if the downvoter could explain so I can improve any future questions. Commented Aug 27, 2018 at 15:32

3 Answers 3

4

It's an stdClass object and you try to access it like an array that's why.

try to access it like $result->title instead.

If it's critical for you to have array format then you can convert stdClass to array by

$data=json_decode(json_encode($result,true),true);

This will provide you an array of your data that you can use the normal methods you know to access it.

Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome i hope i helped you solve your problem
I can only accept it after 3 minutes... there is a time limit ;)
2

You can see your var_dump function is showing that $result is an stdClass() object.

It means that if you want to get a value of that object you need to use $result->title instead of $result['title']

You can also check wpdb::get_results() topic of the WordPress documentation.

You'll find that get_results() method has the second ($output) argument, which is showing of how the result will return to you.

For example:

$results = $wpdb->get_results("SELECT * FROM wp_article_hyperlinks", ARRAY_A);
foreach ($results as $result) {
    // $result['title'] - You can get title value from an associative array
}

$results = $wpdb->get_results("SELECT * FROM wp_article_hyperlinks", ARRAY_N);
foreach ($results as $result) {
    // $result[2] - You can get title value from an numeric array
}

1 Comment

thanks for both the link and the detailed answer. very helpful!
-1

Try below code to solve:

$post_id = 228;

$content_post = get_post($post_id);
$content = $content_post->post_content;

global $wpdb;

$results =$wpdb->get_results("SELECT * FROM wp_article_hyperlinks"); 

foreach ($results as $result){
// $url = $result["url"];
// $title = $result["title"];

    echo '<pre>';
    var_dump($result->title);
    echo '</pre>';
}

3 Comments

@AbraCadaver just trying to say use below code to solve problem.
I get that, but WHY Use Following? This doesn't help without any explanation.
@AbraCadaver Its completely fine if its not accepted but it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.