0

I am able to populate the fields by manually entering the table headings and table data. However i have a large table and I want to dynamically fetch the data from the table. Can someone give me a sample code on how this could be achieved? I want to do it by PDO method.

0

1 Answer 1

1

You may try this

 $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
 $sql = 'SELECT lastname, firstname, jobtitle 
    FROM employees
    ORDER BY lastname';

  $q = $pdo->query($sql);

  $q->setFetchMode(PDO::FETCH_ASSOC);
  <table class="table table-bordered table-condensed">
<thead>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Job Title</th>
    </tr>
</thead>
<tbody>
    <?php while ($r = $q->fetch()): ?>
        <tr>
            <td><?php echo htmlspecialchars($r['lastname']) ?></td>
            <td><?php echo htmlspecialchars($r['firstname']); ?></td>
            <td><?php echo htmlspecialchars($r['jobtitle']); ?></td>
        </tr>
    <?php endwhile; ?>
</tbody>

Sign up to request clarification or add additional context in comments.

1 Comment

I actually know how to do it this way. The problem is I have lots of fields in my table and so manually hardcoding the values is really tedious. I want to know how to dynamically display the headings and values in a table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.