I'm doing a function to return all results from a database. The function in this exact format returns only one result but putting echo
in each statement returns all the results.
How can I make this code that probably has a logic error return all results from 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 ...
This returns all the results ....
while ( $linha = $stmt->fetch ( PDO::FETCH_OBJ ) ) {
echo $linha->post_title . "<br>" . $linha->post_date . "<br>";
}