M
M
Mini MVC2015-10-19 15:51:34
PHP
Mini MVC, 2015-10-19 15:51:34

Why can't I retrieve data from the database?

What am I doing wrong?
Fatal error: Call to a member function fetch() on boolean in /customers/b/c/d/bashlam.com/httpd.www/models/News.php on line 38

public static function getNewsList()
    {
        // Запрос к БД

        $host = 'xxx';
        $dbname = 'xxxxx';
        $user = 'xxxx';
        $password = 'xxxx';
        $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
        
        $newsList = array();
        
        $result = $db->query('SELECT id, title, date, short_content '
                
                . 'FROM news'
                . 'ORDER BY date DESC '
                . 'LIMIT 10');
   
        $i = 0;
        while($row = $result->fetch()){
            $newsList[$i]['id'] = $row['id'];
            $newsList[$i]['title'] = $row['title'];
            $newsList[$i]['date'] = $row['date'];
            $newsList[$i]['short_content'] = $row['short_content'];
            $i++;
        }
        
        return $newsList;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2015-10-19
@alleroy

in Google
"Call to a member function fetch() on boolean"
the first ten of the issue is full

N
ndbn, 2015-10-19
@ndbn

. 'FROM news'
. 'ORDER BY date DESC '

At least it will work
FROM newsORDER BY date DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question