Jump to content

PDO query help?


Mr Chris

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.