SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert Multidimensional array into Mysql Database

    Hi Guys,


    My head is melted... I need to insert a multidimensional array into mysql database. This is what I have could you please have a look and tell me where I'm going wrong.


    Array:

    PHP Code:
    $arrayData = array(
         array(
            
    'id' => 1,
            
    'firstName' => 'Paul',
            
    'surname' => 'Crowe',
            
    'age' => 28,
            
    'gender' => 'male',
            
    'connections' => array(2),
            
    'cities' => array('Dublin' => 80'New York' => 100'Paris' => 95'Madrid' => 100'London' => 80'Barcelona' => 100'Moscow' => 20)
        ),
         array(
            
    'id' => 2,
            
    'firstName' => 'Rob',
            
    'surname' => 'Fitz',
            
    'age' => 23,
            
    'gender' => 'male',
            
    'connections' => array(13),
            
    'cities' => array('Dublin' => 40'New York' => 100'Paris' => 65'Madrid' => 90)
        ),
         array(
            
    'id' => 3,
            
    'firstName' => 'Ben',
            
    'surname' => "O'Carolan",
            
    'age' => null,
            
    'gender' => 'male',
            
    'connections' => array(2457),
            
    'cities' => array('Paris' => 90'Madrid' => 40'London' => 85'Barcelona' => 90'Moscow' => 80)
        ),
         array(
            
    'id' => 4,
            
    'firstName' => 'Victor',
            
    'surname' => '',
            
    'age' => 28,
            
    'gender' => 'male',
            
    'connections' => array(3),
            
    'cities' => array('Paris' => 80'Madrid' => 80'London' => 80'Barcelona' => 80'Moscow' => 40)
        ),
         array(
            
    'id' => 5,
            
    'firstName' => 'Peter',
            
    'surname' => 'Mac',
            
    'age' => 29,
            
    'gender' => 'male',
            
    'connections' => array(3611107),
            
    'cities' => array('Dublin' => 60'New York' => 100'Paris' => 75)
        ),


    foreach(
    $arrayData as $data){

        
    $sql "INSERT INTO people (firstName, surname, age, gender, connections, cities)
        VALUES (
    $data[firstName]$datas[surname]$data[age]$data[gender]$data[connections]$data[cities])";



  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What's the structure of the tables that you're going to insert the data into?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    What's the structure of the tables that you're going to insert the data into?
    id, firstName, surname, age, gender, connections,cities

  4. #4
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are 2 more tables in the database. Cities and connections.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    i don't do php or whatever that was in post #1, but i can see you're trying to store a string of numbers in a single column called connections, and a string of strings in a column called cities

    that's gonna give you some heartaches -- do a search for first normal form
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •