-4

i want to get the result of mysql query into a text, currently it works when i type mysql_num_fields and mysql_num_rows, but it does not work mysql_fetch_array or mysql_fetch_assoc which is the genuine requirement.. can anybody kindly tell me what wrong with this code

$query = "CALL create_report($ID, '$startDate', '$endDate', $endlmt, $position);";

    $Localcon = $this->getConnection();
    $result = mysql_query($query, $Localcon);
    //$debug = mysql_num_rows($result);
    $myFile = "debug.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "-------------\n";
    fwrite($fh, $stringData);
    $stringData = mysql_fetch_array($result);
    fwrite($fh, $stringData);
    fclose($fh);

edited code

$stringData_2 = mysql_fetch_array($result);
    foreach ($stringData_2 as $string) {
        $stringData = "----------------\n";
        fwrite($fh, $stringData);
        fwrite($fh, $string);
    }
Prog_Rookie
  • 438
  • 7
  • 23

1 Answers1

2

The problem is mysql_fetch_array returns an array, while fwrite is expecting a string. Here is the error your code produces:

fwrite() expects parameter 2 to be string, array given in ...

The 2 param of fwrite, $stringData, needs to be a string.

Depending on what you are trying to do, you might start by trying something like this, then play around with other alternatives:

...
foreach($result as $str) {
    fwrite($fh, $str);
}
...

Sidenote: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • 1
    `print_r($result);` and make sure it contains what you think. Then check that `fwrite` works by passing a single, simple string. – Mark Miller Dec 03 '14 at 06:16