Jump to content

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/237101-want-help-with-associative-array/
Share on other sites

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.

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

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

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.

 

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.