-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,193
  • 7
  • 35
  • 75

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
  • 5,464
  • 2
  • 34
  • 62
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
  • 156,878
  • 40
  • 214
  • 345