1

i want write an mysql_fetch_assoc(which returns an associative array) values to a text file, though i want to get the output in a manner like below

ID => 17
CODE => 4
Value => 59559

it just gets printed like follows

17
4
59595
25
0

so this is the code that im currently using to get the result, can anybody help me on this

$query = "CALL pro_details($ID, '$start', '$end', $limit, $pos);";
        $result = mysql_query($query, $con);
        $myFile = "debug.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        while($stringData_2 = mysql_fetch_assoc($result)){
                foreach ($stringData_2 as $string) {
                    fwrite($fh, $string);
                    $stringbreak = "\n";
                    fwrite($fh, $stringbreak);
                }
                $stringbreak = "----------------\n";
                fwrite($fh, $stringbreak);
        }
        fclose($fh);

6 Answers 6

4

Change your fwrite() call to this:

foreach ($stringData_2 as $key=>$string) {
    fwrite($fh, $key ." => ".$string);
    $stringbreak = "\n";
    fwrite($fh, $stringbreak);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Darren and roulie you guys replied first and both answers works... i dont know which answer to accept :P.. u have posted aanswers on exact time
@user3584871 go with roullie, he posted before mine.
3

how about trying

foreach ($stringData_2 as $index => $string) {
    fwrite($fh, $index.'=>'.$string);
    $stringbreak = "\n";
    fwrite($fh, $stringbreak);
}

1 Comment

Darren and roulie you guys replied first and both answers works... i dont know which answer to accept :P.. u have posted aanswers on exact time
1

In foreach loop you can get both the key and values of an array

foreach($variable as $key => $value) {
    // so that u can print both the keys and values
    $key ---> ID,code,Value
    $value ---> 12,4,59559
}

http://php.net/manual/en/control-structures.foreach.php

Comments

1
    while($stringData_2 = mysql_fetch_assoc($result)){
               array_walk( $stringData_2 'displayData');


        }

...

function displayData($item, $key)
{
     fwrite($fh, "$key." => " . $item . "\n");
}

Comments

1

You can solve like this

while($stringData_2 = mysqli_fetch_assoc($result)){
    foreach ($stringData_2 as $key=>$string) {
         fwrite($fh, $key.'=>'.$string);
         $stringbreak = "\n";fwrite($fh, $stringbreak);
    }
   $stringbreak = "----------------\n";
   fwrite($fh, $stringbreak);
}

Comments

0

How about using serialize and unserialize?

 $array["key1"]="value1";
 $array["key2"]="value \"with\" quotes";
 $array["key3"]="value3";

 $tx=serialize($array);
 echo "<LI>serialized:".$tx;

 $test=unserialize($tx);
 foreach($test as $k=>$v) echo "<LI>rebuilt array:[$k] = [$v]";

returns

serialized:a:3:{s:4:"key1";s:6:"value1";s:4:"key2";s:19:"value "with" quotes";s:4:"key3";s:6:"value3";}
rebuilt array:[key1] = [value1]
rebuilt array:[key2] = [value "with" quotes]
rebuilt array:[key3] = [value3]

so therefore you can do the following:

file_put_contents($fn, serialize($array1));
.
.
.
$array2=unserialize(file_get_contents($fn));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.