How do you correctly output data when using $stmt->fetch(PDO::FETCH_ASSOC)
? Right now I\'m getting single words as my result when doing
\"select
$pdo = new PDO(/*Your credentials*/);
$sql = `select name, author, title from d_books`
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($arr = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $arr['name'];
}
or
$pdo = new PDO(/*Your credentials*/);
$sql = `select name, author, title from d_books`
$stmt = $pdo->prepare($sql);
$stmt->execute();
$arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($arr as $titleData) {
echo $titleData['name'];
}
You can always use pdo->query()
method when running a query without any parameters, which is faster, but I do not believe that this happens much(running queries without any parameters).