I connected to the db, pull the data using fetch_assoc()
(fetch_all(NUMB)
is not available on the machine we are working with else this would be less of an issue). I get the returned data and load it into an array. However, it ends up in as an numeric array embedded inside another assoc array. What is the best way to have get the returned data in one, single, simple ASSOC array?
/**
* @Param $conn object [required], $tableName string [required]
* @Return $clmNames array
*/
public function getColumnName($conn = NULL, $tableName = NULL )
{
if ( !is_object( $conn ) || !is_string( $tableName ) )
{
//params are not of correct type. This will eventually throw an error instead of printing to screen
echo("Error: getColumnName returning false");
return false;
}
//create SQL query
$stmt = "SELECT column_name FROM information_schema.columns WHERE table_name = '". $tableName ."' ORDER BY ordinal_position";
//create query
$query = $conn->query($stmt);
//execute query
$tmp = array();
while ($row = $query->fetch_assoc())
{
//load all returned rows into an array
$tmp[] = $row;
}
//clear memory
$query->free();unset($query);
//reCreate Array as a single assoc array instead of a double enbedded one.
$clmNames = array();
foreach( $tmp as $tmpKey=>$tmpValue )
{
foreach( $tmpValue as $tmpValueKey=>$tmpValueValue )
{
$clmNames[] = $tmpValueValue;
}
}
return $clmNames;
}
The above returns the data correctly. However, I would like to be able to remove the entire foreach dealing with $tmp
and $clmNames
if possible.