I have a similiar class with a lot of methods, but getData() only returns the value of the $column parameter.
private $db;
function __construct()
{
$this->db = new PDO('sqlite:\db');
}
public function getData($rowid, $column)
{
$st = $this->db->prepare('SELECT ? FROM tbl WHERE rowid=?');
$st->bindParam(1, $column, PDO::PARAM_STR);
$st->bindParam(2, $rowid, PDO::PARAM_INT);
if ($st->execute())
return $st->fetchColumn();
else
return false;
}
Every other parts of the class and the left out half of getData() works. What’s the problem here?
bindParamis used to bind parameters, not identifiers. The value you bind there will be expanded as:…therefore equivalent to:
You should only use parameters for actual parameters:
If your column is user-supplied and you want to escape it, use the proper escaping function. In this case, it’s
SQLite3::escapeString():If the column is not user-supplied, you don’t really have to escape it.