-3

有没有可能有这个功能:

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”是用户 ID,“what”是我想要的列,例如“title”,所以查询是:

SELECT title FROM projects WHERE fid = 1  

这可能吗?

4

2 回答 2

1

在 PDO 中不能替换表名和列名:

看看这里: PHP PDO 语句可以接受表名或列名作为参数吗?

于 2013-10-30T11:48:25.487 回答
1
function getProjectData($uid) {
    $sql = "SELECT * FROM projects WHERE fid = ?";
    $stmt = $this->db->prepare($sql);
    $stmt->execute(array($uid);
    return $stmt->fetch();
}

这样称呼

$proj_data = getProjectData($uid);

然后获得所需的属性

$title = $proj_data[$what];
于 2013-10-30T11:49:20.407 回答