Count the columns of a Mysql table using PHP

Asked

Viewed 3,859 times

4

As I can count the columns of a Mysql table using PHP, someone knows some command to do this?

I tried to do it this way 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);
  • 1

    Remembering that the use of function mysql_query() nay is recommended due to various safety related reasons. Use PDO or Mysqli in place.

  • 1

    There is also this answer: http://answall.com/questions/13092/visualizar-quantidades-de-colunas-em-cada-tabela/13101#13101

3 answers

5


To count the columns one-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

  • Sorry buddy, I changed Aki is worked, solved my problem! thanks!

1

Buddy, you can use the command:

pg_num_rows

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

or if you are an update you can use this:

pg_affected_rows

 * Retorna o numero de linhas alteradas por essa consulta
 * 
 * 
 * @return  Integer : Numero de linhas  alteradas
  • vlw! is one more way to solve this problem! in this case I can make a query and then use this command to get the number of records of the query right?

  • 1

    pg_* are functions for postgres, this will not return the number of columns of a mysql table.

  • Sorry, I had not seen that you use mysql, but it has similar function. These functions are used after performing the query.

  • without problems, all information is valid, if it did not serve me today can serve tomorrow! vlw!

1

You can use the show Columns,


show columns from my_table;

and then count the lines of the result ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.