简体   繁体   中英

Get Column names of MySQL table with PDO

I'm trying to write a function that returns an array of column names for a MySQL table.

When I use something like this in HeidiSQL

SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE table_schema = 'myDB'
AND TABLE_NAME = 'myTable'

I get exactly what I want. A single column of output showing the column names.

when I took the above code and used it in a PHP program using PDO calls every array value is of the format:

string(xx) "column_name"

I do not want the leading "string(xx)" in front of the column name.

I'm using the pdo function as follows:

fetchAll(PDO::FETCH_COLUMN)

I don't see other PDO fetch options to just give me the column names, without the leading "string(xx)" value.

I could parse the results and strip the leading string(xx) value, but I was wondering if there's an easier/better trick.

Oh, mental lapse... as I dug deeper into my output array, I was thrown off by my output because I was using var_dump. var_dump prefaces the values with the "string(xx)" value. MySQL isn't the one putting the "string(xx)" prefix.

Live and learn...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM