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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Insert Array data into MySQL

    Hoping someone can help me before all my hair is pulled out.

    I have an array named $arrFeeds:

    Code:
    Array
    (
        [0] => Array
            (
                [item id] => 9998
    I cannot seem to get the code correct to insert the actual value (9998) into the database. I currently just get 0 inserted.

    PHP Code:
    mysql_query("REPLACE INTO items (item_id) VALUES('$arrFeeds[0][item id]') ")
    or die(
    mysql_error()); 
    I can do echo $arrFeeds[0]['item id']; and get the value 9998.

  • #2
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    assign it to the mysql as a variable.

    $something = $arrFeeds[0]['item id'];


    mysql_query("REPLACE INTO items (item_id) VALUES('$something') ")

  • #3
    New Coder
    Join Date
    Feb 2010
    Location
    ahmedabad
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try This One



    $i = 0;
    $sql_query = "SELECT * FROM tablename";
    $result = mysql_query($sql_query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){

    $id = $row[id];
    $person = $row[person];



    $arr_id[$i] = $id;
    $arr_person[$i] = $person;

    $i++;


    }

    $smarty->assign("id", $arr_id );

  • #4
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    PHP Won't parse array values inside double quoted strings like it will normal variables.

    Your code won't work, but this, (or seco's) will:

    PHP Code:
    mysql_query("REPLACE INTO items (item_id) VALUES('{$arrFeeds[0][item id]}') "
    Notice the curly braces I've placed around the array value. That's how you do it.

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    This code doesn't work either. Results in an error:

    Parse error: syntax error, unexpected T_STRING, expecting ']' on line 4

    I simplified the original post to have only one key in the inner array. There is actually about 17. So I'll work on a loop making a variable o store the data for each.


  •  

    Posting Permissions

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