Solved

PHP Array Insert into Mysql

Posted on 2015-01-14
4
117 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 500 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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Collecting Customer Data 5 38
Basics of Drool Rule Engine with few samples 2 102
PHP Curl Setup Option 4 128
Unwanted Popup 8 263
A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

738 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