I wanted to do this: all the news from the database to throw in the json-array, and then output all the mess where you want using js. The news has its ID, title and content. Give an example of how all this can be done? I would like a type format: 1{'test', 'desc'},2{'test', 'desc'},3{'test', 'desc'},1{'test', 'desc'} . Naturally not like me, but like json.
Thanks for the answers, I probably have a banal error in the code, but I still do not understand where it comes from.
<?php $main = new PDO('mysql:host=localhost;dbname=world_project', 'world', 'yan6dw'); $result = $db->query("SELECT `id`,`title`,`content` FROM `News` LIMIT 10"); while ($row = $result->fetch()) { $news = [ $row['id'] => [ 'title' => $row['title'], 'text' => $row['content'] ]]; } die ($news); ?> Fatal error: Uncaught Error: Call to a member function query () on null in /home/world/w-0rld.ru/connect.php.l Stack trace: # 0 /home/world/w-0rld.ru/functions .php (2): include_once () # 1 /home/world/w-0rld.ru/index.php(2): include_once ('/ home / world / w-0 ...') # 2 {main} thrown in / home / world / w-0rld.ru/connect.php on line 3
How to fix?
