Jump to content

Storying Array in Database


onlyican

Recommended Posts

You have to serialize the array before storing it in the database and also pass it through mysql_real_escape_string().

[code]<?php
$ary = array('1','two','three','4');
$q = "insert into test_table set `text_field` = '" . mysql_real_escape_string(serialize($ary)) . "'";
?>[/code]

When you retrieve it, you will need to unserialize it before you can use it.

Ken
Link to comment
Share on other sites

I know I can do that, that is what I said I will do

Just thought if there was a way of storing an Array Data into a MySQL Table, it would be easier

I will do something like
$link_string = "";
foreach($links as $link){
$link_string.= $link." |";
}

Then on the reverse

$links = explode(" |", $link_string);

Then I am back in an array


RE: parsing mysql_real_escpae_string

I have my own function that EVERYTHING is run through before looking at a Query string
Link to comment
Share on other sites

If you insist on doing it this way:
[code]<?php
$link_string = "";
foreach($links as $link){
$link_string.= $link." |";
}?>[/code]

instead of using the serialize/unserialize method, use the implode() function instead:
[code]<?php
$link_string = implode('| ',$links);
?>[/code]

since it won't leave the dangling '| ' at the end of the string.

Ken
Link to comment
Share on other sites

hate to complicate things... but if you wanted to save the index as well as the value you could do something like...

:P I'm the king of complicating things! of course.. I use my array indexes way too much so it would explain it I suppose :X

[code=php:0]
<?php
function database_array_to_string($array) {
$num_loops = 0;
foreach($array as $index => $value) {
++$num_loops;
$new_string .= $index . '=>' . $value;
if($num_loops != count($array)) {
$new_string .= '\n';
}
}
return $new_string;
}

function database_string_to_array($string) {
$array = explode('\n', $string);
foreach ($array as $array_chunk) {
list($index, $value) = explode('=>', $array_chunk, 2);
$new_array[$index] = $value;
}
return $new_array;
}

$test_array = array(
'test' => 'array',
'foo' => 'bar'
);

$test_string = database_array_to_string($test_array);
echo $test_string; // test=>array\nfoo=>bar

$test_array = database_string_to_array($test_string);
print_r($test_array); // Array([test] => array [foo] => bar)
?>
[/code]
Link to comment
Share on other sites

why would you reinvent the wheel using that function, genericnumber1, when the serialize function does exactly that, and more...it saves any php object to a string that is suitable for storing in a database or text file...include array indexes.  Nevermind multidimensional arrays..........

http://www.php.net/serialize
http://www.php.net/unserialize

Link to comment
Share on other sites

Instead of using your functions, you can use the [url=http://www.php.net/var_export]var_export()[/url] function to create a readable string which can be "eval"ed to recreate the original array:

[code]<?php
$ary = array('one'=>array('two'=>2,'three'=>3),'zero'=>array('x'=>100,'y'=>200));
$str = var_export($ary,true);
echo '<pre>' . $str . '</pre>';
eval("\$ary1 =" . $str . ';');
echo '<pre>' . print_r($ary1,true) . '</pre>';
?>[/code]

Ken
Link to comment
Share on other sites

Cheers

The reason I want arrays is beause
I have a textbox
Which I make everything in there a full link (change www.site.com to http://www.site.com
by parsing them in arrays

Then
On Export from the database I want to make them an array, so I can loop through them and change
for example
[code]
<?php
foreach($links as $link){
echo "<a href='".$link."' title='".$link."'>".$link."</a>\n";
}

SO I dont need anything special or the key of the array

?>
[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.