user3205047 user3205047 - 6 months ago 17
MySQL Question

CodeIgniter phpMyAdmin - Inserting into table with a space in the name

$this->db->insert('Table One',$data);

This would error. And showing the equivalent sql, insert into.

INSERT INTO `Table` One (`col1`, `col2`) VALUES ('----', '------')

Is there any way to insert?

Maybe the use of a wildcard perhaps? or any special character to substitute space for phpmyadmin to udnerstand? or is it phpmyadmin's fault?


Unfortunately it seems that the active record library doesn't support table names with space in. I looked into the core files and the insert() function calls the protect_identifiers function on the table name and in system/database/DB_driver.php there is the following code

// If the item has an alias declaration we remove it and set it aside.
// Basically we remove everything to the right of the first space
$alias = '';
if (strpos($item, ' ') !== FALSE)
    $alias = strstr($item, " ");
    $item = substr($item, 0, - strlen($alias));

So everything after the first space is removed. So it looks like your only options are to do the query like

$this->db->query('INSERT INTO `Table One` ...');

Or to remove spaces in your table names

Sorry. Hope that helps