【问题标题】:CodeIgniter phpMyAdmin - Inserting into table with a space in the nameCodeIgniter phpMyAdmin - 插入到名称中带有空格的表中
【发布时间】:2014-03-09 17:17:35
【问题描述】:
$this->db->insert('Table One',$data);

这会出错。并显示等效的sql,插入。

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

有没有办法插入?

也许使用通配符?或任何特殊字符来代替 phpmyadmin 到 udnerstand 的空间?还是 phpmyadmin 的错?

【问题讨论】:

  • 你试过 $this->db->insert('Table One', $data, false); ?

标签: php mysql sql codeigniter


【解决方案1】:

不幸的是,活动记录库似乎不支持带空格的表名。我查看了核心文件,insert() 函数在表名和 system/database/DB_driver 中调用了protect_identifiers 函数.php里面有如下代码

// 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));
}

所以第一个空格之后的所有内容都被删除了。所以看起来你唯一的选择是做这样的查询

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

或者删除表名中的空格

对不起。希望有帮助

【讨论】:

    【解决方案2】:

    如果您的表名中有空格,则需要引用全名:

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

    【讨论】:

    • 这不是问题。正如我写的那样。那只是转换或 phpmyadmin 如何使用我上面的代码行。我将表名写为“健康风险”
    【解决方案3】:

    我在进行更新调用时发现了类似的问题

    $this->myBaseTable = "my table";
    $this->db->update($this->myBaseTable); //errors
    

    如果您将刻度添加到表声明中,它似乎可以正常工作,即

    $this->myBaseTable = "`my table`";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多