Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve column names from sqlite3 database with php

Tags:

php

sqlite

I have the following piece of code which displays a list of my current table names in my database which works fine.

<?php // Display all sqlite tables
    $db = new SQLite3('data.db');
    $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table';");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        echo $table['name'] . '<br />';
} ?>

Can you display the list of column names for a table like you can do in mysql? I have tried numerous iterations and all have failed.

Just for the record this is the code I used thanks to esqew for the help:

<?php // Display all sqlite column names for chosen table
    $db = new SQLite3('data.db');
    $tablesquery = $db->query("PRAGMA table_info(USERS)");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        echo $table['name'] . '<br />';
} ?>

All tested and working

like image 630
Sandy Avatar asked Oct 20 '22 08:10

Sandy


1 Answers

Refer to SQLite.org - PRAGMA Statements:

PRAGMA table_info(sqlite_master);

To fit it into your PHP implementation:

<?php // Display all sqlite tables
    $db = new SQLite3('data.db');
    $tablesquery = $db->query("PRAGMA table_info(sqlite_master);");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        echo $table['name'] . '<br />';
} ?>
like image 94
esqew Avatar answered Oct 23 '22 10:10

esqew