Say you type in the shell at the mysql prompt "SHOW COLUMNS FROM table_name;" (DESCRIBE will work too I guess) and you yield:
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| school | tinytext | | | | |
| band | tinytext | | | | |
| director | tinytext | | | | |
| address | tinytext | | | | |
| phone | varchar(14) | | | | |
| email | tinytext | | | | |
| bandsize | int(4) | | | 0 | |
| tickets | int(4) | | | 0 | |
| class | char(2) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
If you want to build a dynamic associative array of the field names listed above then this will accomplish the task:
// CONNECT TO DB //
$query = "SHOW COLUMNS FROM bands;";
$result = mysql_query($query, $link);
$i = 0;
while($data = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($data as $item) {
$build[$i] = "$item";
$i++;
}
}
for($i = 0; $i < count($build); $i=$i+6) {
// NOTICE THE INCREMATION BY 6 TO ONLY POPULATE THE NEW ARRAY ($rebuild) WITH
// ASSOCIATIVE NAMES OF THE field OF THE TABLE AND NOT type, null, key, default or extra
echo $build[$i]."<br />\n"; // SHOWS THE FIELD NAMES
$rebuild["$build[$i]"] = null; // REBUILD AN ASSOCIATIVE ARRAY WITH THE FIELD NAMES
}
print_r($rebuild);
yields:
school
band
director
address
phone
email
bandsize
tickets
class
Array ( [school] => [band] => [director] => [address] => [phone] => [email] => [bandsize] => [tickets] => [class] => )
you can populate the array by saying:
$rebuild['school'] = "Oakville";
$rebuild['band'] = "Marching Tigers";
$rebuild['director'] = "Mr. Mister";
$rebuild['address'] = "Milburn Rd.";
$rebuild['phone'] = "555-5555";
$rebuild['email'] = "oak@ville.com";
$rebuild['bandsize'] = "100";
$rebuild['tickets'] = "15";
$rebuild['class'] = "IIA";
Any suggestions, comments, improvements or usability ideas?
this may be nothing new or exciting but I just thought it up and was curious