onlyican Posted December 13, 2006 Share Posted December 13, 2006 Hey allI want to store an array in a MySQL DatabaseBut I dont know howI tried having the MySQL Field as TEXTbut it just stored ARRAY in the fieldI know I can make it a stringthen make it an array on export from MySQLBut CAN i store an Array in MySQL? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 13, 2006 Share Posted December 13, 2006 erm.... nolet me be more constructive....how about you try combining the values and seperating them with some delimiter into a string... you can then store that string in the database and parse it back into an array later? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
onlyican Posted December 13, 2006 Author Share Posted December 13, 2006 I know I can do that, that is what I said I will doJust thought if there was a way of storing an Array Data into a MySQL Table, it would be easierI 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 arrayRE: parsing mysql_real_escpae_stringI have my own function that EVERYTHING is run through before looking at a Query string Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
onlyican Posted December 13, 2006 Author Share Posted December 13, 2006 Good point KenrbnsnThe only reason I want them in an array is because they are linksso with them being in an array I can just loop through them using foreachand make them a <a href='' link Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 13, 2006 Share Posted December 13, 2006 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]<?phpfunction 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] Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted December 13, 2006 Share Posted December 13, 2006 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/serializehttp://www.php.net/unserialize Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 13, 2006 Share Posted December 13, 2006 because I tend to overlook the simple solutions of course :\ you win this onein my defense, my "serialized" is a little more readable by a person though.. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2006 Share Posted December 13, 2006 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 Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted December 13, 2006 Share Posted December 13, 2006 oh cmon, now yall are just kicking me while I'm down :D Quote Link to comment Share on other sites More sharing options...
onlyican Posted December 14, 2006 Author Share Posted December 14, 2006 CheersThe reason I want arrays is beauseI have a textboxWhich I make everything in there a full link (change www.site.com to http://www.site.comby parsing them in arraysThenOn Export from the database I want to make them an array, so I can loop through them and changefor example[code]<?phpforeach($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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.