?
Solved

PHP Array Insert into Mysql

Posted on 2015-01-14
4
Medium Priority
?
119 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

762 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