Hello all!

i have here a code that will display record from my DB. I would like to ask for help on how will i export the displayed record to excel?

here is my code:

<html>
<head>
<title>Export to Excel</title>
</head>
<body>
<form action="saverec.php" method="post">
<?php
    define ('DB_NAME', 'try1');
    define ('DB_USER', 'root');
    define ('DB_PASSWORD', '');
    define ('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected) {
        die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
    }

    $strSQL = "SELECT * FROM record ORDER BY lname";
    $rs = mysql_query($strSQL);

?>
<table border="1">
<tr>
    <th>Name: </th>
</tr> 
<tr>
    <th>Practice: </th>
</tr> 
<tr>
    <th>Project Name: </th>
</tr> 

<tr>
    <th>Employee Name</th><th>Time Message sent</th><th>Time Replied</th><th>On leave?</th><th>Comments</th>
</tr> 

<?php

    while($row = mysql_fetch_array($rs)) {

?>
<tr>

    <td><?php echo $row['lname'] . ", " . $row['fname']  ?></td><td><?php echo $row['hour1'] . ": " . $row['min1'] . " " . $row['ampm']  ?></td>
    <td><?php echo $row['hour2'] . ": " . $row['min2'] . " " . $row['ampm1']  ?></td><td><?php echo $row['leave'] ?></td><td><?php echo $row['comment'] ?></td>


</tr>    

    <?php    
    } 
    ?>
    </table>

<table>
<tr>
    <th>Sending failed</th>
<td>
        <?php echo $row['sf'] ?>
</td>
    <th>Receiving failed</th>
<td>
        <?php echo $row['rf'] ?>
</td>
    <th>Total</th>
<td>
        <?php echo $row['total'] ?>
</td>
    <th>Sending percentage</th>
<td>
        <?php echo $row['sp'] ?>
</td>
    <th>Receiving percentage</th>
<td>
        <?php echo $row['rp'] ?>
</td>
</tr>

    </table>
    <?php mysql_close(); ?>

    <input type="submit" name="Submit" value="Save" >
</form>
</body>
</html>

Thanks! :)

Recommended Answers

All 4 Replies

Are you looking to export the whole database to excel or just a specific part, if it's the whole thing it would be easier to look at CSV downloading.

You can select specific parts but it might take a tad longer :P

hi bradley_spicer! :)

i would like the displayed result to be exported. right now, i only have 1 record on the table. :)

Take a look at this one as well. This is the tutorial that guided me to accomplish exporting data to excel.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.