N
N
ninja op op2020-05-28 15:33:28
PHP
ninja op op, 2020-05-28 15:33:28

Why does it throw an Illegal string offset error when using foreach?

There is a code:

$query = $mysqli->query("SELECT * FROM `users` ORDER BY `id` DESC LIMIT 10");
                                    
                                        $row = $query->fetch_array();
                                        foreach ( $row as $user){
                                        echo $user['id'];
                                        }

Returns:
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
1
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
1
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
4
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
4
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
4
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
4
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
1
Warning: Illegal string offset 'id' in E:\openserv\OpenServer\domains\kolbasa\index.php on line 165
1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2020-05-28
@kur4chyt

Selects one row from the result set and puts it in an associative array, regular array, or both

https://www.php.net/manual/ru/mysqli-result.fetch-...
And you $rowwork as if there are not columns of one line , but all lines (although you even have a variable named correctly).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question