Count the columns of a MySQL table using PHP

4

How can I count the columns of a MySQL table using PHP, does anyone know of any commands to do this?

I tried to do this but it did not work:

$sql4 = mysql_query("show fields from ".$tabela) or die('erro na query');
$rows4=mysql_fetch_array($sql4);
$total=count($rows4);
    
asked by anonymous 26.06.2014 / 14:11

3 answers

5

To count the columns of a table:

SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'nome_da_sua_base_de_dados' AND
table_name = 'nome_da_sua_tabela';

SQLFiddle Example

    
26.06.2014 / 14:17
1

Friend, you can use the command:

pg_num_rows

 * Retorna o numero de linhas dessa consulta
 * 
 * 
 * @return  Integer : Numero de linhas da consulta

or if it is an update you can use this:

pg_affected_rows

 * Retorna o numero de linhas alteradas por essa consulta
 * 
 * 
 * @return  Integer : Numero de linhas  alteradas
    
26.06.2014 / 15:05
1

You can use show columns,


show columns from my_table;

and then count the result lines ...

    
27.06.2014 / 01:41