-3

Is it possible to have this function:

function getProjectData($uid, $what) {
    $sql = "SELECT ? FROM projects WHERE fid = ?";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(array($uid,$what);
    return $stmt->fetch(PDO::FETCH_LAZY);
}  

"uid" is the user id and "what" would be the column I want e.g. "title" so the query would be:

SELECT title FROM projects WHERE fid = 1  

Is this possible?

Marek123
  • 1,153
  • 6
  • 34
  • 71

2 Answers2

1

Table and Column names cannot be replaced in PDO:

Take a look here: Can PHP PDO Statements accept the table or column name as parameter?

Community
  • 1
  • 1
puelo
  • 4,510
  • 2
  • 34
  • 53
1
function getProjectData($uid) {
    $sql = "SELECT * FROM projects WHERE fid = ?";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(array($uid);
    return $stmt->fetch();
}

call it this way

$proj_data = getProjectData($uid);

and then get desired properties as

$title = $proj_data[$what];
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325