in reply to Inserting Hash Information Into MySQL

Are you using "use strict;" ?

You have 2 variables: $id, and @id - perhaps you are confusing their usage.

Also - you have not shown the code that actually inserts the data into MySQL.

             I hope life isn't a big joke, because I don't get it.
                   -SNL

  • Comment on Re: Inserting Hash Information Into MySQL

Replies are listed 'Best First'.
Re^2: Inserting Hash Information Into MySQL
by Anonymous Monk on Jun 20, 2012 at 15:15 UTC
    I am using strict. The hash uses the values in @id as the key, which is why I used $id. My apologies - the code to enter into MySQL is:
    my $insert_query = "INSERT INTO test(id, stats) VALUES (?, ?);"; $sth = $dbh->prepare($insert_query); while(($key, $value) = each(%hash)){ $sth->execute($key, $value); }
    It inserts the ID without a problem. The only issue is that arrays can't be stored in MySQL, but I don't know how to get the information out of the array to store it otherwise.
      It seems to me that the values in your %hash are array-refs.

      So you cannot simply insert them into the table as is.

      I am not sure what you are trying to achieve but I assume you want something like this:

      while(($key, $value) = each(%hash)){ for my $stat_value (@$value) { $sth->execute($key, $stat_value); } }
        That was what I was looking for, and it worked perfectly. Thank you! I'm sorry for being unclear about what I was trying to achieve.