私はPDOを始めたばかりで、いくつかのチュートリアルで答えを調べましたが、うまくいきません。
私は得た
Notice: Undefined property: PDOStatement::$fetch in E:\-------- on line 22
Result: 1
と
$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) );
私は同じ結果を得る
$sth = $dbh->query($query);
$result = $sth->fetchAll;
と
$sth = $dbh->prepare($query);
$sth->execute();
$result = $sth->fetch;
私が得るのは、結果の数を返す可能性があるということですが、なぜですか? そして、宣言されていない fetch / fetchAll についての通知が表示されるのはなぜですか。私も例外はありません。