DEV Community

Stephen Berry
Stephen Berry

Posted on

Illegal string offset

I am following an online tutorial to build a message form using PHP and Mysqli. Then I hit a problem. I got an 'illegal string offset' message. I checked my code and the code online and all for one line they match.

The code for the array is in a php tag:
...
$rows = [];
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows = $row;
}
...

The code that should display the messages:

...

<img src="http://qlogo3.store.qq.com/qzone/1262283870/1262283870/100?1481718124" alt="photo">

    <a href="#">zipple</a>
Enter fullscreen mode Exit fullscreen mode
        <div class="coninfo">
            <?php echo $row['content']; ?>
        </div>
    <div class="time">2019-12-19  23:46:11</div>
</div><!-- mainInfo end -->
<!-- content_1 end -->
...

There was a <?php endforeach; ?> tag after the last div but this cause a parse error.

I used var_dump($rows); die() in the top php tag where the $rows = []; resides. This gave me data, and using the source page this is what was outputted:

array(3) {
["id"]=>
string(1) "3"
["content"]=>
string(13) "newer message"
["add_time"]=>
string(10) "1619873188"
}

array(3) {
["id"]=>
string(1) "1"
["content"]=>
string(91) "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit"
["add_time"]=>
string(10) "1619869605"
}

If it helps, there are three entries (test) in the database [id] = 1, etc, and text and time in three rows.

I have no idea what is wrong or how to solve it. I would appreciate any help.

Top comments (3)

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!"