2

Can somebody please enlighten me on the use of SHOW COLUMNS specifically in regards to PHP/MySQL because I have not found anything of much use through search.

What I want to do is get all the column names out of a table, then use those to get the correct data from session to do an update.

what i have so far is:

function get_Columns($con, $table){

    $sql_statement = "SHOW COLUMNS FROM " . NAME_TABLE_BASE . $table ;

    $temp = mysql_query($sql_statement) or die(mysql(error)) ;

    return mysql_fetch_array($temp) ;
}
?>

The problem is this is only returning the first column and not all of them. What do I need to change? Also is there anything I can add to $sql_statement to only get the fields and not all the other info like key type etc?

0

2 Answers 2

3

SHOW COLUMNS ... is the oldschool method. For MySQL v5.x installs you can query the database's INFORMATION_SCHEMA database directly for anything related to the databases/tables/fields it's hosting:

select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where (TABLE_SCHEMA = 'your database') AND (TABLE_NAME = 'your table')

There's many more fields with specific field data in that table as well, documented here.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I still have a 4.x vers :(
select version(); will tell you
2

Put it in a while loop to fetch the other column names

$sql_statement = "SHOW COLUMNS FROM " . NAME_TABLE_BASE . $table;
$temp = mysql_query($sql_statement) or die(mysql(error));

while($row = mysql_fetch_array($temp)) {
    print_r($row);
}

1 Comment

Interesting. I tried getting the array and using a foreach but got nothing but the first column. Ill give this a go. Thanks.

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.