Is it possible to list the columns used in a SELECT in mysqli PHP?

2

I have the query

SELECT coluna1, coluna2... FROM tabela 

I wonder if there is any method in the mysqli class to return those columns that were used in the query, even though query does not return records. In a% return% of records I can simply use query in the array_keys() method. But I can not if fetch_assoc() does not return records, since there will be no indexes.

I need to do this because it is a query big, and I would like to do such a dynamic process.

    
asked by anonymous 19.12.2016 / 13:03

1 answer

2

To retrieve the columns used in select use the function / method fetch_fields () which returns information such as name, type, size etc. of the columns.

$db = new mysqli('localhost', 'usuario', 'senha', 'base');

$result = $db->query('SELECT col1, col2, col3 FROM tabela');

$fields  = $result->fetch_fields();

foreach($fields as $item){
    echo $item->name .'<br>';
}

Return is an object with the following properties:

stdClass Object
(
    [name] => col1
    [orgname] => col1
    [table] => tabela
    [orgtable] => tabela
    [def] => 
    [db] => base
    [catalog] => def
    [max_length] => 0
    [length] => 1
    [charsetnr] => 8
    [flags] => 0
    [type] => 254
    [decimals] => 0
)       
    
19.12.2016 / 13:23