CodeIgniter Forums
Newb, having problems with Active Record, results in syntax error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Newb, having problems with Active Record, results in syntax error (/thread-14326.html)



Newb, having problems with Active Record, results in syntax error - El Forum - 12-29-2008

[eluser]Unknown[/eluser]
I've written some php with codeigniter which gets some info from an XML document, and then tries to save it to my db.

However trying to use active record, for inserting to the database, results in a syntax error.

my code:

$data['director'] = $movie->directors->director;
$data['spilletid'] = $movie->movie_duration;
$data['premiere'] = $movie->regions->region->products->product->premiere;
$data['type'] = $movie->regions->region->categories->categorie;

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

And this is the syntax error:

A Database Error Occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'the Universe, 709, Julie Taymor, 134, 2008-06-20, Drama)' at line 1

INSERT INTO `movies_trailer` (`title`, `movie_id`, `director`, `spilletid`, `premiere`, `type`) VALUES (Across the Universe, 709, Julie Taymor, 134, 2008-06-20, Drama)

Can anyone tell me what I am doing wrong?


Newb, having problems with Active Record, results in syntax error - El Forum - 12-29-2008

[eluser]Phil Sturgeon[/eluser]
One annoying thing about using Simple XML is that values do not have the correct types. It's not recognising that the values are strings so fails to add " round them.

Try type-casting:

Code:
$data[‘director’]  = (string) $movie->directors->director;
$data[‘spilletid’]  = (int) $movie->movie_duration;
$data[‘premiere’]  = (int) $movie->regions->region->products->product->premiere;
$data[‘type’]      = (string) $movie->regions->region->categories->categorie;
    
$this->db->insert(‘movies_trailer’, $data);



Newb, having problems with Active Record, results in syntax error - El Forum - 12-29-2008

[eluser]Unknown[/eluser]
Thanks for the help mate, that worked!

They should however consider renameing simpleXML to something along the lines of NotSosimpleXML.


Newb, having problems with Active Record, results in syntax error - El Forum - 12-29-2008

[eluser]Phil Sturgeon[/eluser]
They are renaming it to UsefullButSlightlyOddXML() in PHP 6.


Newb, having problems with Active Record, results in syntax error - El Forum - 08-05-2009

[eluser]Circuitbomb[/eluser]
Great this is exactly the fix I was looking for.

Might I add that type-casting will also work in an array

Code:
$data = array(
        'foo' => (string)$bar,
        'hello' => (int)$world
        );