Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Inserting 2D array into a SQL database

  • 16-01-2007 3:05pm
    #1
    Closed Accounts Posts: 23


    I have a standard php script inserting array values into a mysql database. One of my arrays is 2D, and when i try inserting values into it, what shows up in the database is array[1], array[2] etc, instead of the actual real values. Am i using the wrong insert parameters? Here's my sample code

    Theres a for loop in the main body of my code(which is for a parser incidentally)
    while (strpos($regionline, $c) == true)
    {
    $regionpos = strpos($regionline, $c);
    $regionelement = substr ($regionline, $position, $regionpos);
    $regionarray[$count][$regioncount] = $regionelement;
    $regionline = substr($regionline, $regionpos+1);
    $regioncount = $regioncount + 1;
    }

    And then outside of that there is a for loop, which runs through each element of one (shared) dimension of the array, and then a for loop inside that to exploit the 2D array. Here is my insert for the 2D array
    for ($u=1;$u<$regioncount+1; $u++)
    {
    //echo $Physical_ISRC[$i]. ' ' .$regionarray[$i][$u];
    //echo "<br>";

    $sql = "INSERT INTO regionmeta Values ('$Physical_ISRC[$i]','$regionarray[$i][$u]');";
    if (!mysql_query($sql,$connection))
    {
    die('Error: ' . mysql_error());
    echo "$contentid[$i] not added";
    }
    else
    {
    echo "$i.$u done";
    }

    }

    If i uncomment the echo statements at the start of the second quote, it displays properly the elements, and i've used that exact text for the insert statement, so that doesn't seem to be the problem. Does anybody have any ideas?


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Since when did we not have to specify fields...? Perhaps you don't, but I was always under the impression that the format of an insert query was
    $sql = "INSERT INTO table_name (field1, field2, field3, etc) VALUES ($value1, $value2, $value3, $etc)";
    

    No?


  • Moderators, Politics Moderators Posts: 41,239 Mod ✭✭✭✭Seth Brundle


    Mirror - that is only if you are inserting into each field of the table in their order.
    If you are inserting into some but not all cols of a table then naming the cols as above is the method used.


  • Closed Accounts Posts: 23 doc_1982


    Yeah, there's only 2 rows in the database, and their names and order correspond exactly to the variables being inserted into them.

    Nobody has any ideas about my problem?


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    doc_1982 wrote:
    Nobody has any ideas about my problem?

    Personally, I know next-to-nothing about php. I see no reason why you'd get the values you say (" array[1], array[2] etc") in the db.

    You're inserting two fields, one of which is from a two-dimensional array, so I don't even understand how some funny behaviour could generate "array[2]" from $regionarray[$i][$u].

    If thats whats happening, then you need someone who understands php, not sql. Given that the title of the thread doesn't mention php, that might be part of the deafening silence.

    If what I've described above is not whats happening, then part of your problem is that you've not described your problem accurately.


  • Registered Users, Registered Users 2 Posts: 6,289 ✭✭✭Talisman


    It's been a while since I've used PHP but I can tell you that the solution to your problem is to use PHP's serialize() function when storing the data and unserialize() when retrieving the data. The serialize() function will convert the contents of the array into a string that can be stored in the database.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    http://evolt.org/node/60222

    (1-D array but the same principle)


Advertisement