AL123 Posted February 18, 2011 Share Posted February 18, 2011 New to classes, basic understanding of procedural. I am trying to echo rows from the data base using PDO. Here is the code: <div id="admin"> <form method="POST" action="categoryAdmin.php"> <table> <tr><th bgcolor="#EEEEEE">Cat Id</th> <th bgcolor="#EEEEEE">Title</th> <th bgcolor="#EEEEEE">Description</th> </tr> <?php global $dbh; $sql = "SELECT * FROM categories"; $sth = $dbh->prepare($sql); $sth-> execute(); print_r($sth); $result = $sth->fetchAll(PDO::FETCH_ASSOC); print_r($result); while($row = $result->fetchAll(PDO::FETCH_ASSOC)) {echo "<tr><td>$row[0]<td><td>$row[1]<td><td>$row[2]<td><tr>\n";} ?> <tr><td><input type="text" name="CatId" size="15" maxlength="10"></td> <td><input type="text" name="CatTitle" size="40" maxlength="130"></td> <td><input type="text" name="CatDesc" size="45" maxlength="200"></td> </tr> </table> <input type="hidden" name="addRecord" value="1"> <input type="submit" name="submit" value="Add Category"> </div> print_r($sth); shows: PDOStatement Object ( [queryString] => SELECT * FROM categories ) print_r($result); shows: Array ( [0] => Array ( [categoryId] => Fitness [title] => Sholder Guy [description] => Fix that sholder and more ) ) fatal error: Call to a member function fetchAll() on a non-object in /home/... the line in question is the while loop. I am not sure what to pass it with reguards to PDO. Thanks - AL Quote Link to comment https://forums.phpfreaks.com/topic/228129-non-object-error/ Share on other sites More sharing options...
The Little Guy Posted February 18, 2011 Share Posted February 18, 2011 we will need to see the class too Quote Link to comment https://forums.phpfreaks.com/topic/228129-non-object-error/#findComment-1176528 Share on other sites More sharing options...
trq Posted February 18, 2011 Share Posted February 18, 2011 we will need to see the class too PDO is a PHP extension. @op: You have just shown to yourself (using print_r()) that $result is an array. Why are you trying to use it like an object? Quote Link to comment https://forums.phpfreaks.com/topic/228129-non-object-error/#findComment-1176533 Share on other sites More sharing options...
AL123 Posted February 19, 2011 Author Share Posted February 19, 2011 Sorry for taking so long to reply. The short answer to both of your statements is - I'm a newbee. I am not that familiar with the PDO class I just have sections of it working. A foreach should get me my values. I guess trying to imitate this caused the problem: function locate_table($usrId) { global $dbh; $sql = "SELECT * FROM tblBlog WHERE UserId = $usrId "; $sth = $dbh->prepare($sql); $sth-> execute(); $tbl = $sth->fetchAll(PDO::FETCH_ASSOC);//fetchall return $tbl; } function viewEdit() //replay_table($_SESSION['iden']); { $shoAgain = locate_table($_SESSION['iden']); foreach($shoAgain as $key=>$value) { echo "<ul>"; echo $value['Btype']; echo " <br /> <a href= '../UserView/index.php?id=".$value['id']."&T=".$value['Title']." ' > ".$value['Title']." </a><br /><br /> "; echo "</ul>"; } } I am trying to advance my php skills at home, trial and error. Will mark solved if foreach works. Thanks - AL Quote Link to comment https://forums.phpfreaks.com/topic/228129-non-object-error/#findComment-1176546 Share on other sites More sharing options...
AL123 Posted February 19, 2011 Author Share Posted February 19, 2011 Thanks for the help- AL. Quote Link to comment https://forums.phpfreaks.com/topic/228129-non-object-error/#findComment-1176564 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.