pdo get field names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    pdo get field names

    I'm using PDO for the first time. I want to get the field/column names
    from a SELECT query. I can get the names from the associative array
    returned by fetch(). But how do I get the field names if the SELECT
    query returns no rows? I could do this before with the
    xxxx_fetch_fiel d() functions.

    Here's a condensed version of my code:

    // query the database
    $DbToQuery = new PDO("sqlite::me mory:");
    $DbToQuery->query("CREAT E TABLE MyTable (MyField TEXT)");
    $sql = "SELECT * FROM MyTable";
    $result = $DbToQuery->query($sql);
    // Get the number of columns
    $Cols = $result->columnCount( );
    // Loop through the results
    $countrows = 1;
    while($row = $result->fetch(PDO::FET CH_ASSOC)) {
    if($countrows == 1) {
    // Print column names
    print join(",", array_keys($row ));
    }
    $countrows++;
    // handle the row data
    // ...
    }
Working...