Sign in to follow this  
cdosrunO1

Arrays & SQL

Recommended Posts

Hi guys,

 

   I would thoroughly appreciate help on this matter as I have been stuck for the past few weeks. So, I'm making a tile based game using flash and I've managed to pass the map, in the form of a multi-dimensional array, successfully to PHP. The problem is passing it to SQL and getting it back to flash in the original array format. This has been highly frustrating. The closest I have gotten is getting the array back as a string, but I don't know how serialization works, I haven't had time to learn it and I tried using the JSON as3corelib, but it had too many bugs and it was also frustrating to deal with. 

 

I am asking if anyone knows how to store multi-dimensional arrays (or arrays in general so I can start somewhere) in an SQL database. Right now I am using phpmyadmin for the interface of SQL. If an MD array can be stored, how can be retrieved? 

 

Thanks. 

 

~ Cdosrun 01 ~

Share this post


Link to post
Share on other sites

There are various ways to turn an array into a string with PHP.

 

You can use serialize and unserialize functions (I've never used them, but should work ok). I guess seralize doesn't return something readable by humans.

 

You can use implode and explode functions if you have an array of strings.

 

PHP5 has some functions to encode and decode objects using the JSON format: http://php.net/manual/en/ref.json.php You don't need to understand a lot to use them, but if you want to learn about the JSON format it won't take you a lot of time, is really simple.

 

You can also use print_r or var_dump, but those functions don't have a complementary function. print_r returns valid PHP code, but I've never used it to store into a data base.

Share this post


Link to post
Share on other sites

There are ways around it, but after looking into it for a while I kept running into the conclusion that MySQL is just not designed to store arrays. So while you can find some workarounds (like temporary memory tables and JSON), I decided to just stay away from trying to store arrays (although I have no maps to pass and in my case avoiding arrays ended up in being forced to rethink and improve my db design). Have you considered using something like MongoDB for your game, which actually supports the array data type? You can see their BSON Type documentation for more info.

Edited by Drakonka

Share this post


Link to post
Share on other sites

Most SQL databases also support storing binary data directly, so you don't necessarily have to convert the map data to/from a string if you can store the multidimensional array as a flat buffer (I'm not familiar enough with PHP to know if/how this would be possible). But at the end of day, you'll still need to familiarize yourself with some basic serialization methodologies to get this working properly regardless of the datatype you chose.

 

Also, what kind of map data are you storing? Does it need to be in a database?

Share this post


Link to post
Share on other sites

There are various ways to turn an array into a string with PHP.

 

You can use serialize and unserialize functions (I've never used them, but should work ok). I guess seralize doesn't return something readable by humans.

 

 

I did this and it worked! Thanks! Apparently, the only problem I was having was actually storing the serialized string because the SQL DB kept truncating it. I didn't know you could increase the size of char and varchar, I thought it's max size was 255. Anyway, this fixed the problem and now everything is going smooth, thanks Diego! 

Share this post


Link to post
Share on other sites

Another option is to have a table for the map, with a key composed of the x and y position, and the remaining columns indicate the content of each map cell. Whether this makes sense for you depends on what your project is and how you want to use this map.

Share this post


Link to post
Share on other sites

Another option is to have a table for the map, with a key composed of the x and y position, and the remaining columns indicate the content of each map cell. Whether this makes sense for you depends on what your project is and how you want to use this map.

 

Yeah I thought about doing that, but I figured there could be an easier way to do it; like, I had a feeling. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this