Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:29:59+00:00 2026-05-27T20:29:59+00:00

My question is what is the best and most efficient way to save a

  • 0

My question is what is the best and most efficient way to save a large php array to a mysql table. I know there are a lot of methods out there to do this and I have tried a few of them but I still havent found a method I really feel comfortable using. The main method most people recommend is the serialize() and unserialize() php functions. For some reason this is not working for me and I seem to have tried every way to fix this but idk.

Here is my code:

    // unserialize songs, add new song to end of array, and then re-serialize to add to db
    $cereal = unserialize($dbsongs);
    $pushed = array_push($cereal, $_GET['song']);
    $songs = serialize($pushed);

    //update playlists table
    $stmt = $dbh->prepare("UPDATE playlists SET songs=:songs WHERE title=:title AND user=:user");
    $stmt->bindParam(':user', $_SESSION['username'], PDO::PARAM_STR);
    $stmt->bindParam(':title', $_GET['title'], PDO::PARAM_STR);
    $stmt->bindParam(':songs', $songs, PDO::PARAM_STR);
    $stmt->execute();

So first I unserialize $dbsongs which is the blank list of serialized data from a mysql table. Then i push $_GET[‘song’] to the array with array_push(). Then I serialize that and store it to db. For some reason this isn’t working, but I would also like to know if there is a better way.

Should I try breaking up the php array and saving each item to a table and separating the items by a comma and them breaking that string when I retrieve it from the db?

Or should I just fix this method I am using? Or even add the base64_decode() and base64_encode()? These methods seems kinda old from some of the things I have read… http://www.evolt.org/node/60222

I have seen a method using .implode but I cannot figure out how to use it…Link 1 Link 2. This looks like the most modern approach I have seen. Is this what I should look into further?

Not sure what this method is but it mentions Cakephp which I suppose is a php library. Is this a good method?Link 1

I am going to be storing a lot of items in these lists(like over 1k items regularly), so I’m not even sure if if saving these items to one table field would be appropriate. Would simply making a table for these items be the best method since the list is going to be song large(making a new db entry for each new item added and making a “title” field to link them all together)? I have never experimented with something like this because I could never find enough information about how much data mysql could actually hold without problems arising.

I know there are a lot of questions here but any answers would be greatly appreciated! Thanks in advance!

-BAH

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:29:59+00:00Added an answer on May 27, 2026 at 8:29 pm

    First, let me say that serialize() is the way to go if you want to store PHP arrays in a DB.

    Your problem is that you’ve misused array_push(). If you use it correctly, the serialize() method will work. I’ll address how to fix your code for this purpose in a moment.

    Using serialize and storing the serialized song names in a single TEXT field is actually ideal here because it makes FULLTEXT index searching really simple (I’m assuming MySQL). You can easily find, for example, all lists with song titles containing the word “awesome” when you store the serialized array in a single field.

    An alternative would be to create three tables: one for lists, one for songs and one for links between songs and lists. You could then use a LEFT JOIN query to match songs to lists. In your case, I’d opt for the former option, though.

    The storage method really depends on how you plan to access the data. If you need to be able to match specific song titles quickly and often in a list, the normalized database form mentioned second might be better.

    WHY SERIALIZE IS NOT WORKING FOR YOU RIGHT NOW …

    array_push() returns an integer and the first parameter it receives is passed by reference. This means that the line:

    $pushed = array_push($cereal, $_GET['song']);

    is actually setting $pushed equal to the number of elements in the $cereal array plus one.

    You can correct your problem like so:

    $serial = unserialize($dbsongs);
    $song_count_in_pushed_array = array_push($serial, $_GET['song']);
    $songs = serialize($serial);
    

    UPDATE

    In response to your comment about base64 encoding the serialized data, here’s how you would accomplish this to prevent data corruption with data that isn’t 8-bit clean:

    // For safe serializing
    $encoded_and_serialized = base64_encode(serialize($array));
    
    // For unserializing
    $restored = unserialize(base64_decode($encoded_and_serialized));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found another similar question: Best way to download images from XML content in
What is the most efficient way to split a string by a very simple
When querying with MSSQL what is the most efficient way to grab a single
I mean what the most efficient way to get information about the quantity of
What is the most concise and efficient way to find out if a JavaScript
In Java, what's the most efficient way to return the common elements from two
What would be the best (preferably most efficient) method to do the following: Consider
Similar question: Best Resources for Learning AS3 I am interested in learning AS3 and
This is a question best illustrated by example: User goes to Site A ,
Simple question on best practice. Say I have: public class Product { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.