To get the column names for a MySQL database table in WordPress, use the following:
global $wpdb; $table_name = $wpdb->prefix . 'posts'; foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name ) { error_log( $column_name ); }
Which will result in something like the following being printed to your debug.log:
ID post_author post_date post_date_gmt post_content ...
You get the idea 🙂
This is a trick that shows you so clever to extract column name with all weakness of wordpress;
I think this is only way.
thanks.
I assume this method works correctly and has been tested, but somehow for me it doesn’t return the names of each column.
Does this perhaps have anything to do with wordpress not being abled to handle customly created tables?
It returns NULL for each column name, however it does return the exact amount of columns I have stored in my custom table in the database.
I altered wp-db.php to debug the prepare function to see if anything went wrong with my query.
This is the code I’ve setup:
function prepare( $query, $args = null ) {
if ( is_null( $query ) )
return;
if ( func_num_args() < 2 )
_doing_it_wrong( 'wpdb::prepare', 'wpdb::prepare() requires at least two arguments.', '3.5' );
$args = func_get_args();
array_shift( $args );
// If args were passed as an array (as in vsprintf), move them up
if ( isset( $args[0] ) && is_array($args[0]) )
$args = $args[0];
$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
array_walk( $args, array( $this, 'escape_by_ref' ) );
echo "”;
$test = vsprintf($query, $args);
var_dump($test);
echo “describe returns the following column names in table: ” . $this->form . “\n\r”;
foreach ($this->get_col(“DESCRIBE ” . $this->form . “;”, 0) as $colum)
{
var_dump($column);
}
echo “”;
return @vsprintf( $query, $args );
}
The query I’m currently having trouble with is:
string(236) “INSERT INTO `wp_form` (`personeelsnummer`, `bsn_nummer`, `voornaam`, `voorletters`, `achternaam`, `geslacht`, `geboortedatum`) VALUES (‘123432414′,’weqr21341423′,’21341234′,’23423142′,’2134231432′,’Man’,’03/10/2013′)”
The result from the function above is:
describe returns the following column names in table: wp_form
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
my database returns:
Unknown column ‘personeelsnummer’ in ‘field list’
When I execute describe in phpmyadmin on my table it shows:
Showing rows 0 – 29 ( 34 total, Query took 0.0015 sec)
Including the correct field names.
I’ve been having trouble with this for days now, could you perhaps by sheer chance have a slight idea why that could be?
Are you using $wpdb->prefix before your table names? You’ll want to make sure you use that as part of your queries, otherwise the table names won’t be right. Definitely suggest reading through http://codex.wordpress.org/Class_Reference/wpdb#Run_Any_Query_on_the_Database to learn more 🙂