Mr Chris Posted October 25, 2010 Share Posted October 25, 2010 Hello, Wondering if anyone can help me with a PDO query for outputting a html table. Take this query: function outputSchedule ($dbc) { $QUERY = $dbc->query("SELECT * from tbl"); $QUERY->setFetchMode(PDO::FETCH_ASSOC); $output =' <table> <thead> <tr> <th>Name</th> <th>Weight</th> </tr> </thead> <tbody>'; while($ROW = $QUERY->fetch()) { $output .=' <tr> <td style="width:160px;'.$bg.'">'.$ROW['name'].'</span></td> <td style="width:180px;'.$bg.'">'.$ROW['weight'].'</td> </tr> '; } $output .=' </tbody> </table>'; return $output; } You will see I run through a loop to catch all my records in my database, however my problem is with my table headings and the closing of my table. Say there are no records in the database then the start of the table will automatically print (where the <th>'s are) as well as the end of the table. So I want to basically want to use mysql_num_rows to achieve this. Now if it was in a normal query i'd be fine ($NUM = mysql_num_rows then if $NUM > 0, outputting a message if there are no rows in the database). However, i'm not at all sure how to do it using PDO as what I used threw up errors, can anyone help? Link to comment https://forums.phpfreaks.com/topic/216762-pdo-query-help/ Share on other sites More sharing options...
trq Posted October 25, 2010 Share Posted October 25, 2010 The PDO statement object has a rowCount() method. http://php.net/manual/en/pdostatement.rowcount.php Link to comment https://forums.phpfreaks.com/topic/216762-pdo-query-help/#findComment-1126142 Share on other sites More sharing options...
Mr Chris Posted October 25, 2010 Author Share Posted October 25, 2010 Thanks Thorpe. Sorted now Link to comment https://forums.phpfreaks.com/topic/216762-pdo-query-help/#findComment-1126205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.