Warning: Illegal string offset

I have the following method:

    public function get_content($page_abbr)
    {
        $sql = "SELECT language_abbr 
                     , meta_title
                     , meta_description
                     , meta_keywords
                     , page_name
                     , page_title
                     , page_content
                  FROM page_content
                 WHERE page_name = ?";
                 
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array($page_abbr));
        
        return $stmt->fetch();
    }

Then in my page I have the following:

<section id="content">
  <h2><?php echo $heading; ?></h2>
  <div id="tab-container" class='tab-container'>
    <ul class='etabs'>
      <li class='tab'><a href="#nl">Dutch</a></li>
      <li class='tab'><a href="#en">English</a></li>
    </ul>  
    <div class='panel-container'>
      <?php foreach($meta_details as $value): ?>
      <div id="<?php echo $value['language_abbr']; ?>">
        <h3><span>Titel:</span></h3>
        <br>
        <?php echo $value['meta_title']; ?>  
      </div>
      <?php endforeach; ?>
    </div>
  </div>
</section>

But this is giving me the following error

Warning: Illegal string offset ‘language_abbr’ in C:\wamp\www\subdomains\Admin\private\templates\meta_details.php on line 10

What is wrong with this approach?

Thank you in advance!

Where is $meta_details being populated?

Do a var_dump on $meta_details to find out what’s really in it.

You’re retrieving a single row from the DB, so your foreach loop is setting $value to the contents of each column in turn, which is why trying to do $value['language_abbr'] throws an error.

Looking at your HTML, you’re creating a tab for Dutch and English, so I’m guessing that your get_content() method should actually be calling $stmt->fetchAll(). This will give you an array of DB rows and your foreach loop should then work correctly, with $value containing a single row (an array of column name/value pairs) on each iteration.

You are completely right fretburner. what was I thinking :flushed: Normally it would be indeed a single row but since i fetch both languages it are two . Thanks for the reply

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.