Skip to content

Instantly share code, notes, and snippets.

@Exadra37
Created January 24, 2014 17:21
Show Gist options
  • Save Exadra37/8601897 to your computer and use it in GitHub Desktop.
Save Exadra37/8601897 to your computer and use it in GitHub Desktop.
This is a function easily convertible into a method to use inside of a class that as the name says, get columns names from mysql table, will return all the columns names from a given table in the provided database. The mysql connection used is the native PHP driver PDO.
<?php
/**
*
* @author Paulo Silva(Exadra37) exadra37 in gmail point com
* @package exadra37/PDO
* @version 0.0.1
* @since 24/01/2014
*
*/
// Report all PHP Errors
error_reporting(E_ALL);
// Set execution time to unlimited
set_time_limit(0);
// Use UK Time
date_default_timezone_set('Europe/London');
// THIS IS THE CONTENT OF SETTINGS.INC.PHP:
// define('_DB_NAME_', 'your_database_name');
// define('_DB_TABLE_NAME_', 'your_table_name');
// define('_DB_USER_NAME_', 'your_user_name');
// define('_DB_PASSWORD_', 'your_password');
// define('_DB_HOST_', 'localhost');
require_once 'config/settings.inc.php';
$host = _DB_HOST_;
$database_name = _DB_NAME_;
$table_name = _DB_TABLE_NAME_;
function getColumnsNamesFromMysqlTable($host, $database_name, $table_name)
{
try {
// Creating a connection to mysl database
// If you already have a PDO connection instantiated uncomment the line 43 and do the following:
// - rename the function to getColumnsNamesFromMysqlTable($PDO)
// - Then call the function like this: getColumnsNamesFromMysqlTable($your_pdo_connection)
$PDO = new PDO("mysql:host={$host};dbname={$database_name}", _DB_USER_NAME_, _DB_PASSWORD_);
// Building the query
$query = "
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='{$database_name}'
AND `TABLE_NAME`='{$table_name}'";
// Executing the query, using the previous created connection
$rows =$PDO->query($query);
// One way to fetch the results
// var_dump($result) will return:
// array (size=4)
// 0 => string 'recid' (length=5)
// 1 => string 'rcode' (length=5)
// 2 => string 'storeName' (length=9)
// 3 => string 'scode' (length=5)
foreach ($rows as $key => $row) {
$result[] = $row['COLUMN_NAME'];
}
// The other way to do it
// var_dump($result) will return:
// array (size=4)
// 0 =>
// array (size=1)
// 'COLUMN_NAME' => string 'recid' (length=5)
// 1 =>
// array (size=1)
// 'COLUMN_NAME' => string 'rcode' (length=5)
// 2 =>
// array (size=1)
// 'COLUMN_NAME' => string 'storeName' (length=9)
// 3 =>
// array (size=1)
// 'COLUMN_NAME' => string 'scode' (length=5)
//$result = $rows->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo 'ERROR: '.$e->getMessage();
}
return $result;
}
// How to call the function
$result = getColumnsNamesFromMysqlTable($host, $database_name, $table_name);
// Uncomment following 3 lines to debug results
// echo '<hr>result<pre>';
// var_dump($result);
// echo '</pre><hr>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment