Hi,
I am trying to create a function that will pull data from the database, based on arguments for column and table. I would like the function to return an array of this data.
Is the syntax below correct or is there something major that i am missing?
function buildArray($column, $table){
try
{
$sql = 'SELECT DISTINCT $column, id FROM $table';
$s = $pdo->prepare($sql);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error fetching ' . $column . ' column from ' . $table . 'table';
include 'error.html.php';
exit();
}
foreach ($s as $key => $value)
{
$theArray[] = array($column => $value[$column], 'id' => $value['id']);
}
return $theArray;
}
echo "<pre>";
print_r(buildArray('session', 'sessions'));//the arguments here are column and table names respectively
echo "</pre>";
I have made a change to this line,
$sql = 'SELECT DISTINCT ' . $column . ', id FROM '. $table;
Thanks,