Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting array data into MySQL

    Hello,

    I currently have an array, which each element contains a string similiar to

    Code:
    1,Chicago,CEN,19,12,4,3,27,10,67,53,+14,7-1-2,5-3-1,2-2,7-2-1,Won 4
    2,Pittsburgh,ATL,18,11,4,3,25,8,57,43,+14,6-1-1,5-3-2,3-2,7-2-1,Won 1
    
    // 28 more lines like this, each in an array element
    I also have an array containing the MySQL table headers that I want to input those values to:

    Code:
    $tableHeaders = array("Rank", "Team", "Division", "Games Played", "Wins", "Losses", "Overtime Losses", "Points", "Regulation Plus Overtime Wins", "Goals For", "Goals Against", "Goal Differential", "Home Record", "Away Record", "Shootout", "Last 10", "Streak");
    I tried doing the INSERT statement as I would normally do, but I don't think it's working for these arrays.

    I think it's a foreach statement that I have to do, but I can't find a good example. Can anyone point me in the right direction?
    Last edited by jsquadrilla; 11-17-2011 at 01:58 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    I also have an array containing the MySQL table headers that I want to input those values to:
    "Games Played", "Wins", "Losses", "Overtime Losses", "Points", "Regulation Plus Overtime Wins", "Goals For", "Goals Against"
    Step 1: You can't have spaces in column names. So, you need to change them.
    Step 2: You may use the function implode() to get a comma separated string (to make the query) out of an array.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    Step 1: You can't have spaces in column names. So, you need to change them.
    Step 2: You may use the function implode() to get a comma separated string (to make the query) out of an array.
    Code:
    $tableHeaders = array("Rank", "Team", "Division", "GamesPlayed", "Wins", "Losses", "OvertimeLosses", "Points", "RegulationPlusOvertimeWins", "GoalsFor", "GoalsAgainst", "GoalDifferential", "HomeRecord", "AwayRecord", "Shootout", "Last10", "Streak");
    Fixed that up.

    For your Step 2, I may have said it wrong. Ex:

    I created my array:

    Code:
    $rowArray = array();
    
    $rowArray[0] = "1,Chicago,CEN,19,12,4,3,27,10,67,53,+14,7-1-2,5-3-1,2-2,7-2-1,Won 4";
    
    $rowArray[1] = "2,Pittsburgh,ATL,18,11,4,3,25,8,57,43,+14,6-1-1,5-3-2,3-2,7-2-1,Won 1";
    I looped it, but basically that's what my array contains. So they are already comma-seperated. Or is that the wrong way to go about this?

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect! Got everything to work using implode and changing the way I saved the data. Thanks for the suggestion.

    I have another question (of course!).

    My code for inserting the data is
    Code:
    $result = mysql_query("INSERT INTO standings_20112012 VALUES (".$comma_separated.")");
    Which works fine for inserting. But now that all the data is inserted, I need to either update/overwrite etc. as new data is available.

    I'm guessing this is an easy question, but how can I go about doing that? Would it just be the UPDATE statement?


  •  

    Posting Permissions

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