lostprophetpunk Posted September 13, 2009 Share Posted September 13, 2009 Hello there, I am just trying out PDO in php. However, I have come across a problem. How would I echo out all of the data that is in the table? The code below only echos out the first entry in the database. <?php // username and password taken out of code /*** mysql hostname ***/ $hostname = 'localhost'; /*** mysql username ***/ $username = ''; /*** mysql password ***/ $password = ''; try { $dbh = new PDO("mysql:host=$hostname;dbname=blogv2", $username, $password); /*** connected ***/ } catch(PDOException $e) { echo $e->getMessage(); } /*** The SQL SELECT statement ***/ $sql = "SELECT * FROM posts"; /*** fetch into an PDOStatement object ***/ $stmt = $dbh->query($sql); /*** echo number of columns ***/ $obj = $stmt->fetch(PDO::FETCH_OBJ); /*** loop over the object directly ***/ echo $obj->title.'<br />'; echo $obj->entry.'<br />'; echo $obj->poster; ?> If anyone could help out, that would be great as I am new to PDO. Quote Link to comment https://forums.phpfreaks.com/topic/174086-echo-all-results-from-pdo-query/ Share on other sites More sharing options...
RichardRotterdam Posted September 13, 2009 Share Posted September 13, 2009 try fetchAll instead of fetch to return an array $obj = $stmt->fetchAll(); Quote Link to comment https://forums.phpfreaks.com/topic/174086-echo-all-results-from-pdo-query/#findComment-917771 Share on other sites More sharing options...
corbin Posted September 13, 2009 Share Posted September 13, 2009 Or do stuff inside of a loop: while($obj = $stmt->fetch(PDO::FETCH_OBJ)) { //echo stuff out in here. } Quote Link to comment https://forums.phpfreaks.com/topic/174086-echo-all-results-from-pdo-query/#findComment-917775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.