Freitag, 22. Dezember 2017

MySQL count database table columns with PHP

Count all columns of a MySQL database table with PHP and get out the number.

Procedural style

<?php

$select_count_columns = "
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE table_schema = 'database_name'
AND table_name = 'table_name'
";

$database_connect = mysqli_connect("localhost", "user", "password", "database_name");

$database_connect_select_count_columns = mysqli_query($database_connect, $select_count_columns);

$database_connect_select_count_columns_result = mysqli_fetch_array($database_connect_select_count_columns);

$database_connect_select_count_columns_result_number = $database_connect_select_count_columns_result[0];

echo $database_connect_select_count_columns_result_number;

mysqli_close($database_connect);

?>