muzaffar
muzaffar

Reputation: 1736

codeigniter avoid appending quotes to a value

I'm new to codeigniter. The problem I'm facing is that I'm storing a value in database and codeigniter append single quotes around my value and if I've single quotes around my value the SQL server doesn't store it and prompts error.

I'm actually getting data from a table like this

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

The next thing is that I've to manipulate the value of a column shape before inserting this data into another table. For this I get the shape attribute of $poi object and manipulate it like below

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

Now, when I try to save this $poi to database like below

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

The query looks like

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

You can see it has appended the single quotes around the shape value and also added single quotes where ever the signle quotes comes inside value. I want to avoid this. I want the statement like below

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

I've looked around but couldn't find any solution. If anyone knows, please help me

Edited

if I use

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

the resultant query looks like

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

Upvotes: 0

Views: 450

Answers (1)

Zaragoli
Zaragoli

Reputation: 686

Try this:

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

Upvotes: 1

Related Questions