codeigniter避免将引号附加到值

时间:2016-04-30 08:06:22

标签: sql sql-server codeigniter activerecord

我是codeigniter的新手。我面临的问题是我在数据库中存储一个值,并且codeigniter在我的值周围附加单引号,如果我在我的值周围单引号,则SQL服务器不会存储它并提示错误。

我实际上从这样的表中获取数据

$this->db->select('priority, address, shape.STAsText() shape');
$this->db->from('POI');
$this->db->where("OBJECTID = '$id'");
$poi = $this->db->get()->row();

接下来,我要在将此数据插入另一个表之前操纵列形状的值。为此,我获得了shape对象的$poi属性,并按照下面的操作进行操作

$poi->shape =  "geometry::STGeomFromText('$poi->shape',4326)";

现在,当我尝试将此$poi保存到数据库时,如下所示

$this->db->insert('poi_logs',$poi)

查询类似于

INSERT INTO "poi_logs" ("priority", "address", "shape")
    VALUES
(6, 'Street 40 Margalla Town Phase 2 Islamabad', 'geometry::STGeomFromText(''POINT (73.106225740025934 33.668451250424937)'',4326)');

您可以看到它在shape值附近添加了单引号,并且还添加了单引号,其中单词引号的内部值。我想避免这种情况。我想要下面的声明

INSERT INTO "poi_logs" ("priority", "address", "shape")
    VALUES
(6, 'Street 40 Margalla Town Phase 2 Islamabad', geometry::STGeomFromText('POINT (73.106225740025934 33.668451250424937)',4326));

我环顾四周但找不到任何解决方案。如果有人知道,请帮帮我

被修改

如果我使用

$poi->shape =  $this->db->escape("geometry::STGeomFromText('$poi->shape',4326)");

结果查询看起来像

INSERT INTO "poi_logs" ("priority", "address", "shape" )
    VALUES
(6, 'Street 40 Margalla Town Phase 2 Islamabad', '''geometry::STGeomFromText(''''POINT (73.106225740025934 33.668451250424937)'''',4326)''')

1 个答案:

答案 0 :(得分:1)

试试这个:

$poi->address = $this->db->escape($poi->address);
$poi->shape =  "geometry::STGeomFromText('$poi->shape',4326)";
$this->db->set($poi, '', false);
$this->db->insert('poi_logs');