Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Array Insert into Mysql

Posted on 2015-01-14
4
Medium Priority
?
120 Views
Last Modified: 2015-01-19
I need to put the information in the array into a mysql table. Table1
$types = $rets->GetMetadataTypes();
      print_r($types);

This is what the output is. How do i loop through the array
inserting Resource and the value and the Data Elements and their values.

(
    [0] => Array
        (
            [Resource] => ActiveAgent
            [Data] => Array
                (
                    [0] => Array
                        (
                            [ClassName] => Agent
                            [VisibleName] => Agent
                            [StandardName] => Agent
                            [Description] => Active Agent
                            [TableVersion] => 01.03.02650
                            [TableDate] => Thu, 15 Jan 2015 03:07:39 GMT
                            [UpdateVersion] =>
                            [UpdateDate] =>
                        )

                )

        )

    [1] => Array
        (
            [Resource] => Office
            [Data] => Array
                (
                    [0] => Array
                        (
                            [ClassName] => Office
                            [VisibleName] => Office
                            [StandardName] => Office
                            [Description] => Office
                            [TableVersion] => 01.03.02650
                            [TableDate] => Thu, 15 Jan 2015 03:07:39 GMT
                            [UpdateVersion] =>
                            [UpdateDate] =>
                        )

                )

        )

    [2] => Array
        (
            [Resource] => Property
            [Data] => Array
                (
                    [0] => Array
                        (
                            [ClassName] => A
                            [VisibleName] => Residential Improved
                            [StandardName] => ResidentialProperty
                            [Description] => Residential Improved
                            [TableVersion] => 01.03.02650
                            [TableDate] => Thu, 15 Jan 2015 03:07:39 GMT
                            [UpdateVersion] =>
                            [UpdateDate] =>
                        )

                    [1] => Array
                        (
                            [ClassName] => C
                            [VisibleName] => Land
                            [StandardName] => LotsAndLand
                            [Description] => Land
                            [TableVersion] => 01.03.02650
                            [TableDate] => Thu, 15 Jan 2015 03:07:39 GMT
                            [UpdateVersion] =>
                            [UpdateDate] =>
                        )

                    [2] => Array
                        (
                            [ClassName] => D
                            [VisibleName] => Commercial/Industry
                            [StandardName] => CommonInterest
                            [Description] => Commercial/Industry
                            [TableVersion] => 01.03.02650
                            [TableDate] => Thu, 15 Jan 2015 03:07:39 GMT
                            [UpdateVersion] =>
                            [UpdateDate] =>
                        )

                )

        )

)
0
Comment
Question by:leadcodev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 2000 total points
ID: 40550530
Let's assume that you already have the table set up and a database connection. You will want to have insert statements that look something like this:

foreach($types as $type) {

      $resource = $type['Resource'];
      foreach ($type['Data'] as $data) {
      print "INSERT table (resource, classname, visiblename, standardname, description, tableversion, tabledate, updateversion, updatedate) VALUES ('$resource', '{$data['ClassName']}', '{$data['VisibleName']}', '{$data['StandardName']}', '{$data['Description']}', '{$data['TableVersion']}', '{$data['TableDate']}', '{$data['UpdateVersion']}', '{$data['UpdateDate']}');\n";
}
}

Note that this is susceptible to SQL insertion attacks, so the data should be trusted and should not have escape characters. There are better/safer ways, but this should work.
0
 

Author Comment

by:leadcodev
ID: 40550603
Actually I will be creating the table from the array from a webservice. Sorry I wasn't clear on that.  Any ideas on how to do that.
0
 

Author Comment

by:leadcodev
ID: 40550605
Setting up a dynamic process.
0
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 2000 total points
ID: 40551257
If you don't know the schema in advance, you are probably better off using a NoSQL database like MongoDB or others. MySQL is better suited for relational tables.

There are two other options, neither are good.
1. You could create a table with two fields, the first is an index and the second is a text field containing a serialization of your data (i.e. use PHP serialize or  XML or json)
2. Before each insert, you could check to see if the field exists and add it if it does not.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question