+ Reply to Thread
Results 1 to 8 of 8

Thread: Problem inserting serialized array

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Problem inserting serialized array

    I can't get my head round this....

    I have an array which I want to store to the DB (I won't even go into the reasons for this!) but am having problems.

    The serialized array string contains double quotes, so I have to be careful.

    PHP Code:
    $query_insert_summary sprintf('INSERT INTO IMGCOMPS (DESCRIPTION, ANALYSIS) VALUES ("Test","%s")',$serialisedSummaryArray);
    $result_insert_summary mysql_query($query_insert_summary$discountdomains) or die(mysql_error()); 
    I have tried without the sprintf method as well.

    I'm getting back an error.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Highlights";i:0;s:9:"Lowlights";i:0;s:5:"Greys";d :49;s:5:"Flesh";d:7.00000000000' at line 1
    I tried the following structure too...

    PHP Code:
    $query_insert_summary 'INSERT INTO IMGCOMPS (DESCRIPTION, ANALYSIS) VALUES ("Test",$serialisedSummaryArray)'
    But I get back

    Unknown column '$serialisedSummaryArray' in 'field list'
    I can't wrap the serialized array in double or single quotes here without causing problems.

    Any ideas?

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Problem inserting serialized array

    Use a PDO prepared query. Since values are sent out-of-band, metacharacters are a non-issue.

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Problem inserting serialized array

    Hmm - good try but PDO is disabled with my host.

    I might have to do implode/explode

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Problem inserting serialized array

    PHP Code:
    $query_insert_summary =
     
    "INSERT INTO IMGCOMPS (DESCRIPTION, ANALYSIS) VALUES ('Test','$serialisedSummaryArray')"
    Does this give you an error?
    Nothing is always absolutely so.

  5. #5
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Problem inserting serialized array

    OMG - Yes - I thought this was the first option I tried.... obviously not lol.

    Thanks.

  6. #6
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Problem inserting serialized array

    Hmm - good try but PDO is disabled with my host.

    I might have to do implode/explode
    Use mysqli then, I believe that has support for prepared queries too. Frankly any host that disables PDO is more than likely just being too lazy to update their PHP. So basically, avoid using the old mysql driver like the plague, it is twice outdated and lacks a considerable amount of security you could get from using the newer techniques.
    If you insist upon using the mysql driver, look at using mysql_real_escape_string on the input, this will escape all the quotes and other characters in the string.

    Edit: yes, a quick check reveals mysqli does have support for preparing queries, so I strongly suggest you try and move your code over to using it.
    Last edited by lemon-tree; 10-31-2010 at 02:46 PM.

  7. #7
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Problem inserting serialized array

    Thanks for all the help.

    I would have used individual fields, but I am writing an image analysis class and I wanted to provide an image "fingerprint" for each image type to compare with. Each fingerprint is kinda complex!
    Last edited by learning_brain; 10-31-2010 at 02:58 PM.

  8. #8
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Problem inserting serialized array

    If anyone ever asks what NoSQL (schemaless) databases are good for, this is a perfect example. It's probably not an option (given that even PDO isn't supported), but Apache CouchDB is probably the optimal store for this kind of data.

    (If you've never worked with a document store before, it takes some time to wrap your head around it. CouchDB is part of the standard Ubuntu distro if you want to play with it. If your app gets enough traction to require the kind of server space you can control, it will probably allow you to include features that are impractical using an RDBMS. CouchDB, and its spiritual parent, the Notes data store, will let you do marvelous things with arbitrary data, at the cost of some efficiency in dealing with regularly-structured data.)
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  2. PHPMailer / MySQL Array Problem
    By stevet70 in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 06-02-2009, 11:37 AM
  3. Problem With An Array Encoder + Decoder
    By masterjake in forum Free Hosting
    Replies: 1
    Last Post: 11-24-2008, 06:27 AM
  4. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM
  5. Image Inserting Problem
    By nickmagic in forum Free Hosting
    Replies: 2
    Last Post: 02-10-2008, 12:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers