1

I need to build a PHP class which allows me to retrieve the names of all columns of a certain MySQL Database.

Suppose I had the following strucutre:

| ID | Name | Surname | Age | Sex |

My script should provide an array as:

0 => ID
1 => Name
2 => Surname
3 => Age
4 => Sex

Ideas? :)

0

3 Answers 3

4
$result = mysql_query("SHOW COLUMNS FROM sometable");
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

3

Use

DESCRIBE TableName

or

SHOW COLUMNS FROM TableName

Comments

1

You can use the INFORMATION_SCHEMA COLUMNS table, e.g. (using PDO) :

$stmt = $pdo->prepare('
    SELECT
        COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
    FROM
        INFORMATION_SCHEMA.COLUMNS
    WHERE
        table_name=:tablename
        AND table_schema=:databasename
');
$stmt->execute( array(':tablename'=>'foo', ':databasename'=>'bar') );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.