2016-10-31 57 views
-1

我正在学习PDO,并且我一直在收到此错误,我做了一些搜索,没有任何结果解决问题。这里是我使用的代码:使用PDO时出错:致命错误:调用成员函数prepare()null

的index.php :::

require_once 'database/Connection.php'; 
require 'Task.php'; 
$pdo = Connection::make(); 

$statement = $pdo->prepare("select * from todos"); 
$statement->execute(); 
$tasks= $statement->fetchAll(PDO::FETCH_CLASS, 'Task'); 

<ul> 
     <?php foreach ($tasks as $task) : ?> 
      <li><?= $task->description; ?></li> 
     <?php endforeach; ?> 
    </ul> 

Connection.php :::

class Connection { 

    public static function make() { 
     try { 
       $pdo = new PDO('mysql:host=127.0.0.1;dbname=mytodo', 'root', 'mysql'); 
      } catch(PDOException $e) { 
       die($e->getMessage().' DB Could not find'); 
      } 
    } 

} 
+0

尝试将'27.0.0.1'更改为'localhost' –

+0

你得到的错误是什么? –

+0

@ S.I。更改为localhost,同样的错误 –

回答

0

使用return关键字为:

class Connection { 

    public static function make() { 
     try { 
       return $pdo = new PDO('mysql:host=127.0.0.1;dbname=mytodo', 'root', 'mysql'); 
      } catch(PDOException $e) { 
       die($e->getMessage().' DB Could not find'); 
      } 
    } 

}

+0

哦......老兄。感谢很多兄弟 –

相关问题