I'm working automatically and dynamically generating SQL queries for inserting CSV data into a selected database. Now I have a list of 10 different databases. Now I'm curious if it is possible to build a part of my query dynamically (the table names) by fetching the column name from the database?
This is the code I have right now but it doesn't quite work:
function getTableDetails($table_name) {
global $con, $user;
$describeTable = mysqli_query($con, "DESCRIBE " . $user . "." . $table_name);
$fields = [];
while($show = mysqli_fetch_fields($describeTable)) {
$fields['column_name'][] = $show['COLUMN_NAME'];
$fields['column_length'][] = $show['CHARACTER_MAXIMUM_LENGTH'];
$fields['column_type'][] = $show['COLUMN_TYPE'];
}
return $fields;
}
How I try to fetch them
$table = getTableDetails($settings_type);
foreach ($table['column_name'] as $columnName) {
print_r($columnName);
}