phpmysqlfwrite

Writing an php associative array to a text file


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);

Solution

  • how about trying

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