MySQL SHOW COLUMNS unexpected behavior

I have the following PHP code:

$res = mysqli_query($this->get_connection(),"SHOW COLUMNS FROM " . $this->get_table());
$mat = mysqli_fetch_assoc($res);
var_dump($mat);

The connection is OK and it is returning data, so we can skip this part.

The problem is that, as I understand, SHOW COLUMNS should return one line for each column in my table. But when I run this code against tb_categoria, whose description is:

CREATE TABLE IF NOT EXISTS `tb_categoria` (
    `id_categoria` int(11) NOT NULL AUTO_INCREMENT COMMENT 'CHAVE DA TABELA',
    `descricao_categoria` varchar(100) NOT NULL COMMENT 'NOME DA CATEGORIA (PRODUTO)',
    `situacao_categoria` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'SITUACAO DA CATEGORIA ( 0 INATIVO - 1 ATIVO)',
    `imagem_categoria` varchar(100) NOT NULL DEFAULT '' COMMENT 'IMAGEM PRINCIPAL DA CATEGORIA (PRODUTO)',
    `legenda_categoria` varchar(500) DEFAULT NULL COMMENT 'LEGENDA DA CATEGORIA (HOME)',
    `resumo_categoria` text COMMENT 'RESUMO DA CATEGORIA (DETALHE)',
    `ordenacao_categoria` int(3) DEFAULT NULL COMMENT 'ORDEM DA CATEGORIA',
    `description_categoria` varchar(255) DEFAULT NULL,
    `keywords_categoria` varchar(255) DEFAULT NULL,
    `title_categoria` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id_categoria`),
    KEY `situacao_categoria` (`situacao_categoria`) USING BTREE,
    KEY `descricao_categoria` (`descricao_categoria`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=193 ;

it returns information just about 'id_categoria', the first column.

Has anyone faced this problem before? Any suggestions?

Upvotes: 0

Views: 88

Answers (1)

Tarun Upadhyay
Tarun Upadhyay

Reputation: 724

Use a while loop to traverse through all the columns.

    $res = mysqli_query($this->get_connection(),"SHOW COLUMNS FROM " . $this->get_table());
   while($mat = mysqli_fetch_assoc($res)){
    var_dump($mat);
   }

Upvotes: 1

Related Questions