1

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();
    }
4

1 回答 1

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();
    }
}
于 2013-08-02T17:59:56.343 回答