My question is the best and most efficient way to store a large php array in mysql table. I know that there are many ways to do this, and I tried several of them, but I still haven't found a way that I really feel comfortable. The main method recommended by most people are the serialize () and unserialize () php functions. For some reason, this does not work for me, and I seem to be all trying to fix it, 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 an empty list of serialized data from mysql table. Then I press $ _ GET ['song'] on the array with array_push () . Then I serialize this and save it in db. For some reason this does not work, but I would also like to know if there is a better way.
Should I try to split the php array and save each element in the table and separate the elements by comma, and they break this line when I extract it from db?
Or should I just fix this method that I use? Or even add base64_decode () and base64_encode () ? These methods seem old from some of the things I read ... http://www.evolt.org/node/60222
I saw a method using .implode , but I can’t figure out how to use it ... Link 1 Link 2 . This seems like the most modern approach I've seen. Is this what I should study further?
Not sure if this method, but it mentions CakePHP, which I suppose is a php library. Is this a good method? Link 1
I am going to store a lot of elements in these lists (for example, more than 1k elements regularly), so I'm not even sure if saving these elements in one field of the table would be appropriate. Would it be simple to create a table for these elements as the best method, since the list would be big sandy (creating a new db entry for each new element added and creating 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 mysql data could actually run without problems.
I know there are a lot of questions here, but any answers would be greatly appreciated! Thanks in advance!
-BAH