Solved

PHP Array Insert into Mysql

Posted on 2015-01-14
4
110 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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

808 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