Is it possible to bind a table name?
No.
You have to whitelist table names. I doubt you want to let a user to browse any table from your database.
AndGiven you haveare using a class, it will be no-brainer to format identifiers manuallyadd a table name as well. There is a tag wiki with exampleproperty. Why not read it first?
Update: As you can see, PDO turns out toIt will be inconvenient for real life tasks. Sosimple, you have to have a more intelligent abstraction library to handle MySQL querieselegant and safe. Here is Create an example using the safeMysqlabstract parent class, which will make your code dramatically shorter: first
class form{
abstract publicclass functionabstractTable __construct($table){
globalprivate $db;$table;
return $db->getAll("DESCRIBE ?n", $table);
private }$db;
}
2 notes:
- I've removed the second parameter as there is no code in your function that uses it.
- NEVER connect in the class. Use an already opened connection instead. Or you will kill your MySQL server with so many connects.
Exclude implemented version
class form {
public function __construct($table,$skip = array("id")$pdo){
global$this->db $db;= $pdo;
}
$datapublic =function arraydescribe();
{
$res =return $db->query("DESCRIBE ?n", $table`$this->table`");
while($row = $db->fetch>fetchAll($res)) {;
}
}
Then create a specific class for your table
class someTable ifextends (!in_array($row['Field'],$skip))abstractTable {
private $data[]$table = $row;'sometable';
}
and so you will be able to get the required list of columns
$pdo = new }PDO(...);
$table = returnnew $data;someTable($pdo);
$fields = }
}$table->describe();
Howeversimple, such class seldom can be used as intended - there are always a lot of exceptions and manual formatting to make it usableconcise, powerful, safe.