PDO::FETCH_ASSOC我想知道每次设置为默认值时是否需要使用ATTR_DEFAULT_FETCH_MODE?
这是我的连接:
$host = 'localhost';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset";
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
以及使用 fetch 的示例查询:
$stmt = $pdo->prepare('SELECT * FROM users');
$stmt->execute();
$user = $stmt->fetch();
和$user = $stmt->fetchAll();
不写就够了吗$user = $stmt->fetchAll(PDO::FETCH_ASSOC);
慕妹3242003