I'm trying to get a multidimensional array returned from a function.
I want to have the column names as first level and data as second level.
array[columnName][data]
I can var_dump()
the function and get the correct output, but I can't echo()
the output.
<?php
function fetch_columname($schema,$tablename)
{
try
{
$dbh = new PDO("mysql:host=host;dbname='dbname', 'user', 'pass'");
$query = $dbh->prepare("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`=:schema AND `TABLE_NAME`=:tablename");
$query->bindParam(":schema", $schema);
$query->bindParam(":tablename", $tablename);
$query->execute();
$result = array();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$result[] = $row;
}
$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
return $result;
}
$columname = array();
$columname = fetch_columname('new_site','users');
$count = count($columname);
for($i=0;$i<$count;$i++)
{
echo $columname[$i];
}