hii guys i was looking forward to creating an excel sheet .. the entries of which shouldf be the data in the database..i already have the data in the database .. juz wanna transfer n display them on the excel sheet... i dont wanna look beyond php for tis task!!!!! please help me out..

Dani AI

Generated

As clarified, the goal is a read-only, spreadsheet-like display of your database using only PHP. That can be done neatly and safely with a small server-side script. Below is a minimal, practical pattern using PDO (recommended) that prints a proper HTML table, escapes output to avoid XSS, and keeps the approach driver-agnostic by deriving headers from the first fetched row.

<?php
$dsn = 'mysql:host=localhost;dbname=your_db;charset=utf8mb4';
$pdo = new PDO($dsn, 'db_user', 'db_pass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->query('SELECT * FROM your_table LIMIT 100'); // add WHERE/LIMIT for large tables
$first = $stmt->fetch(PDO::FETCH_ASSOC);

if ($first) {
    echo "<table>\n<thead><tr>";
    foreach (array_keys($first) as $col) {
        echo "<th>" . htmlspecialchars($col, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8') . "</th>";
    }
    echo "</tr></thead>\n<tbody>\n";

    // print first row
    echo "<tr>";
    foreach ($first as $v) { echo "<td>" . htmlspecialchars($v, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8') . "</td>"; }
    echo "</tr>\n";

    // remaining rows
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<tr>";
        foreach ($row as $v) { echo "<td>" . htmlspecialchars($v, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8') . "</td>"; }
        echo "</tr>\n";
    }
    echo "</tbody></table>";
} else {
    echo "No rows found.";
}

Notes and quick tips: set the charset in the DSN to avoid encoding issues; escape all cell output with htmlspecialchars (php.net htmlspecialchars); avoid SELECT * for big tables and implement server-side pagination (LIMIT/OFFSET) if results are large; never interpolate unvalidated table/column names—use a whitelist if those identifiers come from users. For download needs later, a simple CSV output via fputcsv to php://output is easy. See the PDO manual for connection options and error modes. This gives a safe, readable table without leaving PHP; add small CSS or server-side sorting/pagination for a nicer experience.

Recommended Answers

All 6 Replies

You can use mysql database and load them directly to excel..simple and easy way..

This questions seems to be asked on a regular basis. I have a little routine that can write an Excel spreadsheet () but if you don't even want to write a program use phpMyAdmin and export to excel.

i have tried both the two things that u told but all i need as the output is that the data shud appear on the spreadsheet...

Sorry, your problem isn't clear. If you use either of these approaches, you will get the data in the spreadsheet. If you don't want to actually create a spreadsheet and just want to display a spreadsheet-like screen display of the data, then you are probably looking at using some Ajax. In that case, have a look at jquery
http://www.flexigrid.info/

hi chrishea.. actually wht i wanted was that the details of the database should appear in a tabular formwith the rows and columns... is it possible only using php codings..

Your very first sentence in the first post was
"i was looking forward to creating an excel sheet".
That's why it took a number of posts to get down to what you really need.

If all you want to do is to display (not change) then you can certainly do that with PHP. If you want to generalize it you can query MySQL for the column names and use that to determine how many columns you need and to put the names at the top of each column. Then you can successively read each row and fill in the values. What you will have is a low-end version of phpMySQL browse. As long as you aren't looking for any of the spreadsheet-like features such as colors, calculations or update then no problem.

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.