2

I'm a bit new to codeigniter and I'm trying to run this simple query:

DESCRIBE `table_name`;

I tried this:

$sql = 'DESCRIBE ?';
$desc = $this->db->query($sql, $table)->result();

Which creates this query:

DESCRIBE 'table_name';

As you can see, the wrong quotes are being outputted when I bind the $table variable; they are value quotes ('), not table quotes (`). Am I doing this wrong?

thank you!

Stéphane Lam
  • 107
  • 2
  • 8
  • Related, if it's of interest, I wrote a *Describe All Tables* in [this Answer](http://stackoverflow.com/a/38679580). – Drew Jul 31 '16 at 00:55

1 Answers1

3

CodeIgniter's query bindings will escape things for you. It assumes that the data is a value, not a table name.

You're gunna have to escape the value yourself.

$table = $this->db->escape_str($table);
$sql = "DESCRIBE `$table`";
$desc = $this->db->query($sql)->result();
gen_Eric
  • 223,194
  • 41
  • 299
  • 337