salman233 Posted May 22, 2011 Share Posted May 22, 2011 I WANT HELP WITH ASSOCIATIVE ARRAY THERE IS ARRAY LIKE THIS $ARRAY= array( 'dd'=>1, 'dde'=>233, 'qww'=2231 ) i want to store this array in database which data type i should i use want to use mixed data mixed string-numeric type which data type is suitable and i want to print indexes only but values only what should i do Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/ Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 Not quite understanding your question by seems youre wanting to do store the actual array within your database? If you do then you need to serialize it first. Then when you get it out of the database you'll need to unserialize it to convert it back to an array. The data type you'll want to use would be TEXT. Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1218639 Share on other sites More sharing options...
salman233 Posted May 22, 2011 Author Share Posted May 22, 2011 okay i understand about the serialize concept i want to know which mysql datatype should i use to store these mixed values like ddew,1.6773 for example VARCHAR,INT WHICH datatype and how to print the indexs values for associative array for example array ('ddd'=>23333 how to print ddd Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1218640 Share on other sites More sharing options...
wildteen88 Posted May 22, 2011 Share Posted May 22, 2011 To store the serialized array you'll want to use the TEXT data type. To get array indexes (keys) you can use array_keys. Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1218661 Share on other sites More sharing options...
salman233 Posted May 23, 2011 Author Share Posted May 23, 2011 thanks wildteen the record is not being saved it is showing in some increpted form when i serialize it but it is not displayed or being saved in database Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1218950 Share on other sites More sharing options...
wildteen88 Posted May 23, 2011 Share Posted May 23, 2011 Post the code you're using to store the serialized array within the database. Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1219134 Share on other sites More sharing options...
salman233 Posted May 24, 2011 Author Share Posted May 24, 2011 THIS IS THE ARRAY $real= array ( 'USD'=>1, 'PAK'=>1.6323, 'GBP'=>1.02544 ); $abc= serialize($real); NO I STORE IT BUT IT WON,T BE STORED $dql = "INSERT INTO countrytype (`ID`,`fromcountry`,`tocountry`) VALUES ('','".$abc."','".$abc."')"; mysql_query($dql) or die(mysql_error()); NOW TO RETRIVE IT for($i=0;$i<$row=mysql_fetch_assoc($aws);$i++) { $des=unserialize($row['fromcountry']); $des=explode(",",$row['fromcountry']); echo $row['ID']; echo $des; AND PRINTING INDEXES $k=1; foreach($des as $fef=>$dwe) { echo "<option name='ad[]' value='$dwe'>$dwe</option>"; $k++; } } BUT IT DON,T PRINT INDEXES AND DON,T STORE IT IN DATABASE HELP ME OUT BRO Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1219403 Share on other sites More sharing options...
wildteen88 Posted May 24, 2011 Share Posted May 24, 2011 When using the serialized array in your query you should first pass it to mysql_real_escape_string. $abc= mysql_real_escape_string( serialize($real) ); $dql = "INSERT INTO countrytype (`ID`,`fromcountry`,`tocountry`) VALUES ('','".$abc."','".$abc."')"; When you are retrieving the serialized array this line is not needed, remove it. $des=explode(",",$row['fromcountry']); This line $des=unserialize($row['fromcountry']); Will restore the array. Quote Link to comment https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/#findComment-1219674 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.