致命错误:在非对象上调用成员函数prepare()

时间:2013-08-02 17:16:07

标签: php function prepare

http://steliyanportfolio.zzl.org/

try {
    $pdo = new PDO ( 'localhost', '869276_amsteljlo', 'st08553813', 'steliyanportfolio_zzl_myportfolio' );
} catch ( PDOException $e ) {
    exit ( 'Database error.' );
}

投资组合php

include_once ('connection.php');
var_dump($pdo);
class Portfolio {
    public function fetch_all(){
        global $pdo;
        $query = $pdo->prepare("SELECT * FROM portfolio");
        $query->execute();
        return $query->fetchAll();
    }

1 个答案:

答案 0 :(得分:0)

替换:

try {
    $pdo = new PDO ( 'localhost', '869276_amsteljlo', 'st08553813', 'steliyanportfolio_zzl_myportfolio' );
} catch ( PDOException $e ) {
    exit ( 'Database error.' );
}

使用:

$dsn = 'mysql:dbname=steliyanportfolio_zzl_myportfolio;host=127.0.0.1';
$user = '869276_amsteljlo';
$password = 'st08553813';

try {
    $pdo = new PDO($dsn, $user, $password);
} catch ( PDOException $e ) {
        exit ( 'Database error.' );
    }

总的来说,您的代码应如下所示:

$dsn = 'mysql:dbname=steliyanportfolio_zzl_myportfolio;host=127.0.0.1';
    $user = '869276_amsteljlo';
    $password = 'st08553813';

try {
    $pdo = new PDO($dsn, $user, $password);
} catch ( PDOException $e ) {
        exit ( 'Database error.' );
    }

    var_dump($pdo);
class Portfolio {
    public function fetch_all(){
        global $pdo;
        $query = $pdo->prepare("SELECT * FROM portfolio");
        $query->execute();
        return $query->fetchAll();
    }
}