Welcome Guest, Not a member yet? Register   Sign In
Newb, having problems with Active Record, results in syntax error
#1

[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?
#2

[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);
#3

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

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

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

[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
        );




Theme © iAndrew 2016 - Forum software by © MyBB