Fatal error: Call to a member function fetchAll() on a non-object in pdo

前端 未结 4 1423
日久生厌
日久生厌 2020-12-21 18:40

I\'m new to pdo just tried the following and getting fatal error.

$pdo = new pdo(\'mysql:localhost;widget_corp;charset=utf-8\', \'root\', \'\');
$query = $pd         


        
4条回答
  •  感情败类
    2020-12-21 19:43

    $pdo->query() will return false if the query fails. You're not initiating the pdo correctly, and you probably want to check if the query didn't return an error, so:

    $pdo = new pdo('mysql:host=localhost;dbname=widget_corp;charset=utf-8', 'root', '');
    $query = $pdo->query("SELECT * FROM `users`");
    if($query !== false)
    {
        $result_array = $query->fetchAll(PDO::FETCH_ASSOC);
    }
    

提交回复
热议问题