{"id":517,"date":"2011-03-26T19:09:36","date_gmt":"2011-03-26T18:09:36","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=517"},"modified":"2013-12-29T20:54:39","modified_gmt":"2013-12-29T19:54:39","slug":"mysql-get-names-of-columns-and-put-them-in-an-array","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2011\/03\/mysql-get-names-of-columns-and-put-them-in-an-array\/","title":{"rendered":"MySql &#8211; get names of columns and put them in an array"},"content":{"rendered":"<p>Sometimes I need to get all the names of the columns in an sql table, most often to use the names of the columns as names of variables as it&#8217;s easier to use than simple $row[1], $row[2], etc. Let&#8217;s take this WordPress table &#8216;wp_comments&#8217; as an example:<\/p>\n<table class=\"data\" id=\"table_results\">\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Null<\/th>\n<th>Key<\/th>\n<th>Default<\/th>\n<th>Extra<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"odd\">\n<td>comment_ID<\/td>\n<td>bigint(20) unsigned<\/td>\n<td>NO<\/td>\n<td>PRI<\/td>\n<td><i>NULL<\/i><\/td>\n<td>auto_increment<\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_post_ID<\/td>\n<td>int(11)<\/td>\n<td>NO<\/td>\n<td>MUL<\/td>\n<td>0<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_author<\/td>\n<td>tinytext<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><i>NULL<\/i><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_author_email<\/td>\n<td>varchar(100)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_author_url<\/td>\n<td>varchar(200)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_author_IP<\/td>\n<td>varchar(100)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_date<\/td>\n<td>datetime<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td>0000-00-00 00:00:00<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_date_gmt<\/td>\n<td>datetime<\/td>\n<td>NO<\/td>\n<td>MUL<\/td>\n<td>0000-00-00 00:00:00<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_content<\/td>\n<td>text<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><i>NULL<\/i><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_karma<\/td>\n<td>int(11)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td>0<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_approved<\/td>\n<td>varchar(20)<\/td>\n<td>NO<\/td>\n<td>MUL<\/td>\n<td>1<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_agent<\/td>\n<td>varchar(255)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>comment_type<\/td>\n<td>varchar(20)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"even\">\n<td>comment_parent<\/td>\n<td>bigint(20)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td>0<\/td>\n<td><\/td>\n<\/tr>\n<tr class=\"odd\">\n<td>user_id<\/td>\n<td>bigint(20)<\/td>\n<td>NO<\/td>\n<td><\/td>\n<td>0<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you want to use the names of the columns as the names of variables returned by a database query, you&#8217;d have to copy&amp;paste them from the table one by one, which is time consuming and very inconvenient, of course. This simple code:<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\">$column_names = array();\r\n$query = \"SHOW COLUMNS FROM wp_comments\";\r\n$result = mysql_query($query); \r\n\r\nwhile($column = mysql_fetch_array($result)) {\r\n\t$column_names[] = $column[0];\r\n\t}\r\n$columns_string = implode(', $', $column_names);\r\nprint $columns_string;<\/pre>\n<p>will print out a list of variables based on the column names of the given table. You can take all the names at once now and paste the list here instead of doing it one by one.<\/p>\n<pre lang=\"php\">list($comment_ID,\r\n        $comment_post_ID,\r\n        $comment_author,\r\n        $comment_author_email,\r\n        $comment_author_url,\r\n        $comment_author_IP,\r\n        $comment_date,\r\n        $comment_date_gmt,\r\n        $comment_content,\r\n        $comment_karma,\r\n        $comment_approved,\r\n        $comment_agent,\r\n        $comment_type,\r\n        $comment_parent,\r\n        $user_id) = $row;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes I need to get all the names of the columns in an sql table, most often to use the names of the columns as names of variables as it&#8217;s easier to use than simple $row[1], $row[2], etc. Let&#8217;s take &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2011\/03\/mysql-get-names-of-columns-and-put-them-in-an-array\/\"> <span class=\"screen-reader-text\">MySql &#8211; get names of columns and put them in an array<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,22],"tags":[25,4],"class_list":["post-517","post","type-post","status-publish","format-standard","hentry","category-programming","category-web-related","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":25,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions\/946"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}