• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving an Array to Database

#1
[eluser]RMinor[/eluser]
Maybe it's just late and my brain is fried, but I can't seem to figure out how to take the contents of the array below and save it into my database table. Here is the array:
Code:
print_r($insert_data);

Array ( [photo] => Array ( [0] => large_pic24.jpg [1] => large_pic25.jpg [2] => large_pic26.jpg [3] => large_pic27.jpg ) [thumbnail] => Array ( [0] => large_pic24_thumb.jpg [1] => large_pic25_thumb.jpg [2] => large_pic26_thumb.jpg [3] => large_pic27_thumb.jpg ) )

The controller code will take each photo and insert it into the database. Here is the model that will be saving each photo and thumbnail:
Code:
public function insertPhoto($photo, $thumbnail, $person_id)
{
    $data = array (
        'photo_photo'   => $photo,
        'photo_thumbnail'  => $thumbnail,
        'photo_person'   => $person_id
    };
    $this->db->insert('photo', $data);
    if ($this->db->affected_rows() == 1) {
        return TRUE;
    }
    return FALSE;
}

Can somebody help me out here? Thanks in advance.

#2
[eluser]skunkbad[/eluser]
Use serialize()

#3
[eluser]RMinor[/eluser]
[quote author="skunkbad" date="1349502911"]Use serialize()[/quote]

When I echo the serialized form of $insert_array I get this:
Code:
a:2:{s:5:"photo";a:4:{i:0;s:14:"large_pic2.jpg";i:1;s:15:"large_pic21.jpg";i:2;s:15:"large_pic22.jpg";i:3;s:15:"large_pic23.jpg";}s:9:"thumbnail";a:4:{i:0;s:20:"large_pic2_thumb.jpg";i:1;s:21:"large_pic21_thumb.jpg";i:2;s:21:"large_pic22_thumb.jpg";i:3;s:21:"large_pic23_thumb.jpg";}}

What would be the proper syntax for using that?

#4
[eluser]GrahamDj28[/eluser]
I use json_encode and json_decode.
Found it to be more reliable then serialize

Save to database
Code:
$string = json_encode($array);

json_encode wil return a string just like serialize does.
You can now save it to your DB. Don't forget to escape your string when saving it!

When retriving it from the database
Code:
$result = json_decode($db_result['your_json_string']);

This wil turn the json string into an object. To get the array do

Code:
$result = (array)json_decode($db_result['some_column']);

I don't know how you are returning your DB results, so this is just as an example.

Hope this helps you out!

#5
[eluser]CroNiX[/eluser]
[quote author="RMinor" date="1349503911"][quote author="skunkbad" date="1349502911"]Use serialize()[/quote]

When I echo the serialized form of $insert_array I get this:
Code:
a:2:{s:5:"photo";a:4:{i:0;s:14:"large_pic2.jpg";i:1;s:15:"large_pic21.jpg";i:2;s:15:"large_pic22.jpg";i:3;s:15:"large_pic23.jpg";}s:9:"thumbnail";a:4:{i:0;s:20:"large_pic2_thumb.jpg";i:1;s:21:"large_pic21_thumb.jpg";i:2;s:21:"large_pic22_thumb.jpg";i:3;s:21:"large_pic23_thumb.jpg";}}

What would be the proper syntax for using that?[/quote]Then you'd use unserialize() to decode it back to the original array.

#6
[eluser]skunkbad[/eluser]
[quote author="GrahamDj28" date="1349506290"]I use json_encode and json_decode.
Found it to be more reliable then serialize[/quote]

Would you please explain? I've got huge projects where serialized data is used extensively and stored in the db. Never had any problems. The one thing to know for beginners is that if you try to unserialize something that isn't serialized, php fails miserably. For my own needs, when I need to test if something is serialized, I use Wordpress' is_serialized() function. I took the function and put it in a helper. Works great.

One of the benefits of serialization is that it retains your data types, whereas json encoding turns everything into a string. Also, json decoding properly can be confusing for a beginner, because by default json_encode() only goes one level deep.


#7
[eluser]GrahamDj28[/eluser]
Well first of all by default I am able to encode multidimensional arrays with no problems.
Second, I know when a result is encoded or not, as it is my own application.
Third, I have been using json so long now I can't recall what the reason was for switching from serialize. But I think it had something to do with the fact I use Doctrine in combination with CI and things would go wrong on saving and or reading from the DB.

I am not saying that serialize is a wrong method. Just saying that json worked better in my situations as my project are also always UTF-8.

#8
[eluser]jmadsen[/eluser]
[quote author="GrahamDj28" date="1349506290"] To get the array do

Code:
$result = (array)json_decode($db_result['some_column']);

[/quote]

Actually, json_decode() takes a second param; true will return an array

http://www.php.net/manual/en/function.json-decode.php

#9
[eluser]RMinor[/eluser]
After reading the replies, I think I misstated my question. I don't want to store the entire array in the database, I want to break the array apart and insert the values into their own fields in my database.

#10
[eluser]skunkbad[/eluser]
[quote author="RMinor" date="1349546243"]After reading the replies, I think I misstated my question. I don't want to store the entire array in the database, I want to break the array apart and insert the values into their own fields in my database.[/quote]

If your array is an associative array, and the keys are the names of the fields in the database, you'd do something like this

Code:
$insert_arr = array(
  'name'     => 'Brian',
  'username' => 'skunkbad'
);

$this->db->insert('table_name', $insert_arr);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.