未定义fetchAll并在返回的PDOStatement中获取

Sdl*_*ion 4 php mysql pdo undefined

我刚刚开始使用PDO,并在几个教程中查找答案,但我无法使其工作.

我有

Notice: Undefined property: PDOStatement::$fetch in E:\-------- on line 22 
Result: 1
Run Code Online (Sandbox Code Playgroud)

$dsn = "mysql:host=localhost;dbname=the_database;";
try {
    $dbh = new PDO($dsn, "root", "");
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e){
    die( "failed conexion: ".$e->getMessage() );
}


$query = "SELECT MAX(price) AS max, MIN(price) AS min FROM cellphones";
try {
    $sth = $dbh->prepare($query);
    $sth->execute();
    $sth->setFetchMode(PDO::FETCH_ASSOC);
    $result = $sth->fetchAll;
    }
catch(PDOException $e){
    echo $e->getMessage();
    }
die( "<br />Result: ".print_r($result, true) );
Run Code Online (Sandbox Code Playgroud)

我得到了相同的结果

$sth = $dbh->query($query);
$result = $sth->fetchAll;
Run Code Online (Sandbox Code Playgroud)

$sth = $dbh->prepare($query);
$sth->execute();
$result = $sth->fetch;
Run Code Online (Sandbox Code Playgroud)

我得到的是,它可能会返回结果的数量,但为什么呢?为什么它给我一个关于fetch/fetchAll甚至没有声明的通知.我也没有任何例外.

Ray*_*Ray 8

您需要使用带有paranthesis的方法调用:

$sth->fetchAll();
Run Code Online (Sandbox Code Playgroud)

或$ sth-> fetch();

不只是

$sth->fetchAll;
Run Code Online (Sandbox Code Playgroud)

PHP认为你试图点击一个名为fetchAll的属性!