Count the columns of a MySQL table using PHP

Question:

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

I tried to do this but it didn't work:

$sql4 = mysql_query("show fields from ".$tabela) or die('erro na query');
$rows4=mysql_fetch_array($sql4);
$total=count($rows4);

Answer:

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';

Example in SQLFiddle

Scroll to Top