Jump to content

WANT HELP WITH ASSOCIATIVE ARRAY


salman233

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
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

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.