DEV Community

Discussion on: Illegal string offset

Collapse
 
grahamthedev profile image
GrahamTheDev

There appears to be bits missing here (try reformatting your post) - you are initially adding everything to an array $rows.

But what I can't see is where you then iterate over those rows using something like

foreach($rows as $row){
  // within the loop now so we can access each $row.
}
Enter fullscreen mode Exit fullscreen mode

That might be where your mistake lies.

Illegal String Offset errors in circumstances like this are when you try to access a property that does not exist in an array (or more specifically that you have a string and are trying to access an array property on it).

If you aren't within a loop you are essentially trying to access $row when it is technically undefined, so it converts it to a string thinking it is a new variable and then tries to find the element at position "content", which doesn't exist.

Sorry if that makes it sound complicated and I don't think I explained it well, but in essence $row is not an array at the point you are trying to use it.

Collapse
 
steveby profile image
Stephen Berry

The code is exactly as shown on the instructions I am following, so I have no idea how they get the results shown. I will figure out how to do this. As the result will show the number of messages above the messages. So if there are three messages this will be a number.

Collapse
 
steveby profile image
Stephen Berry

Not long after posting the reply, I looked carefully at the code on the video and mine, and it turns out I had made a mistake. I had $rows = $row;, which should have been $rows[] = $row;. Once I corrected this, the results I got matched the video. In the words of a wise man "Doh!"