samedi 12 décembre 2015

how to export all sqlite table in php

this is my following coding to export the table to csv file. I using the xampp server connect to sqlite. After i click the button on my interface, it link to a blank page and it is no work. At my csv file also empty. SELECT * FROM sqlite_master where type='table'; this is my query to select all table.

    <?php 

    $table_selection_messages = "";
    error_reporting(0);
        if (isset($_POST['submit'])) {
            $conn = new   
//connection of sqlite
    PDO('sqlite:D:\ProgramFiles\RoomMangement\RoomManagement.sqlite');
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//my query to select all the table in sqlite
            $selected_table = $_POST['table'];
            $sql = "select * from sqlite_master where type=$selected_table";
            $result = pg_query($conn, $sql);
            $error = pg_last_error($conn);

            if(preg_match('/ERROR/i', $error)){
                $table_selection_messages = 
                "Table of $selected_table does not exist. Please contact administrator.";
            }else{
//transfer the data base to the file
                $fp = fopen('csv file/'.$selected_table.' '.date('Y-m-d').'.csv', 'w');

                while($row = pg_fetch_assoc($result))
                {
                    fputcsv($fp, $row);
                }

                fclose($fp);
            }

            pg_close($db);
        }
    ?>

Aucun commentaire:

Enregistrer un commentaire