1

I'm using Zend Framework (1.12) and I'd like to create a table based on a JSON file. I've already created the table and its fields (they're all longtext for now), all it has to do is insert them into the right columns. I followed these examples:

http://www.daniweb.com/web-development/php/threads/381669/json-to-mysql-with-php (second post) Parse JSON to mySQL

The problem is that my JSON is constructed differently (mine has a "root element" called Actie, don't really know the right term) which contains an array with all the objects. Currently, I'm using this code:

$actieurl = "http://creative3s.com/thomas/nmdad/actie.json";
        $my_arr = json_decode(file_get_contents($actieurl));

        $db = new Zend_Db_Adapter_Pdo_Mysql(array(
            'host' => 'localhost',
            'username' => 'root',
            'password' => NULL,
            'dbname' => 'zf-tutorial'
            ));

        foreach($my_arr as $key => $value){
            $sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'"; 
        }

        $sqlclause = implode(",",$sql);
        $query = "INSERT INTO `testerdetest` SET $sqlclause";
        $db->query($query);

But I'm getting an error saying that I'm passing an array:

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\Users\Thomas\Documents\GitHub\NMDAD-testing\application\controllers\IndexController.php on line 29

Does anyone know how to solve this with a JSON of this format? Keep in mind that I cannot alter the JSON in any way. Extra links:

JSON: http://creative3s.com/thomas/nmdad/actie.json Table structure: https://i.sstatic.net/HPKVL.png

5
  • Post your JSON. It's not decoding to a flat array like you expect. Also why are you calling mysql_query and $db->query()? Commented Jan 22, 2013 at 15:33
  • 1
    nice SQL injection hole... you're trying to escape the value... but you've forgotten to escape the key. And did you read the error message? you're trying to escape an ARRAY. Commented Jan 22, 2013 at 15:33
  • @MarcB I know that, it's trying to parse the Actie array which contains all my objects. The problem I'm having is that I don't know how to properly change the code to reflect this change of structure in the JSON. I realize it's trying to parse the entire Actie array. I updated my answer to include the JSON at the bottom of the question. Commented Jan 22, 2013 at 15:39
  • @haiqt: you'd need to loop over that sub-array and do whatever you need to. you've provided NO details as to your table structure. Commented Jan 22, 2013 at 15:43
  • @MarcB I've included the table structure in my question as well. You can find at i.imgur.com/KtXeEuw.png. I followed the tutorials in my question and they said to create columns with the same name as all the fields in the JSON. Commented Jan 22, 2013 at 15:46

1 Answer 1

2

Your json data has the top level key 'Actie', so you need to be looping through $my_arr->Actie.

You can simplify your code to just:

$actieurl = "http://creative3s.com/thomas/nmdad/actie.json";
$my_arr = json_decode(file_get_contents($actieurl));

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host' => 'localhost',
    'username' => 'root',
    'password' => NULL,
    'dbname' => 'zf-tutorial'
));

foreach($my_arr->Actie as $row){
    $db->insert('testerdetest', (array)$row);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.