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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.