1
I’m doing a function to return all the results of a database. The function in this exact format returns me only one result but putting echo
in each statement returns me all the results.
How can I make this code that probably has a logic error return me all the results of the database?
try {
// PDO em ação!
$pdo = new PDO ( "mysql:host=localhost;dbname=nomedobanco", "usuariodobanco", "senhadobanco", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM wp_posts ORDER BY post_date ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {
$conteudo = $linha->post_title . "<br>" . $linha->post_date . "<br>";
return $conteudo;
}
} catch ...
Thus returns all results ....
while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {
echo $linha->post_title . "<br>" . $linha->post_date . "<br>";
}
fetchAll()
in place offetch()
– rray