A function to pull data from DB

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){
              $sql = 'SELECT DISTINCT $column, id FROM $table';
              $s = $pdo->prepare($sql);
            catch (PDOException $e)
              $error = 'Error fetching ' . $column . ' column from ' . $table . 'table';
              include 'error.html.php';

          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;


add this after your execute()

$rows = $s->fetchAll(PDO::FETCH_ASSOC);

 foreach ($rows as $row)
                  $theArray[] =  array('col1'=> $row[$column], 'col2' => $row['id']);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.