Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

php writing to a file

file.csv is writable

nothing is being written to .csv file
not even 'hello'

<?php 
require_once('../common.php');     
error_reporting(E_ALL); 
     
    $query = "SELECT * FROM products"; 
    $result = mysql_query($query); 
        $sql= "SELECT * FROM products"; 
    $result = dbquery($sql); 
    $fp = fopen('file.csv', 'w'); 
    echo 'hi';
    fputcsv($fp,'hello');
    while ($row = mssql_fetch_array($result)){ 
        echo 'line: ';
        echo "<br>";
        echo "quotes";
        fputcsv($fp, $row); 
    } 

    fclose($fp);  
?>

Open in new window

Avatar of Ovunc Tukenmez
Ovunc Tukenmez
Flag of Türkiye image

Second parameter of the fputcsv() function must be an array:

<?php
    error_reporting(E_ALL);
    $fp = fopen('file.csv', 'w');
    fputcsv($fp, array('hello'));
    fclose($fp);
?>
use fwrite instead of fputcsv
fwrite($fp, "hello\n");
@rgb192, all of the PHP functions are documented on the online PHP man pages.  For example, you can find fputcsv here:
http://us.php.net/manual/en/function.fputcsv.php

Have a look at that and get used to the format of those pages.  You can look up any function any time just by putting its name into the search box on the upper right of the PHP man pages.  As a matter of good practice you should always look up EVERY function that is unfamiliar to you.  Read the documentation, examples, and user-contributed notes.  It is one of the best online learning resources you will ever see.
>>mssql
???  try:
<?php 
require_once('../common.php');     
error_reporting(E_ALL); 
     
    $query = "SELECT * FROM products"; 
    $result = mysql_query($query); 

    $fp = fopen('file.csv', 'w') or die('Unable to open file'); 
    echo 'hi';
    fputcsv($fp,'hello');
    
    //you need a 'y' not an 's' in mysql_fetch_ array
    while ($row = mysql_fetch_array($result)){ 
        echo 'line: ';
        echo "<br>";
        echo "quotes";
        fputcsv($fp, $row); 
    } 

    fclose($fp);  
?>

Open in new window

Now having said that (and I hope you take it to heart) there is another thing that is wrong with the code here, and it is easy to fix, and it is necessary to fix.  The code does not take into account any of the return values from the functions!

Example:
$result = mysql_query($query);

How do you know if that worked?  What is in $result?  It might be a resource or it might be FALSE, but the code example above does not know - it just tries to use the $result var without testing for success.

So while fputcsv might have failed all on its own accord, you might have other errors that caused it to appear to fail - because your script sent it bogus input.

Also, fputcsv() returns a value that you want to test - if it fails, it returns FALSE.  Quote the online man page:
"Return Values
Returns the length of the written string or FALSE on failure."

A quick count of the code there shows 6 (six) different functions that provide return values, and none of them are being tested for success.  That needs to be changed.  Every one of them should have something like this to figure out what is going on.

Best of luck with it, ~Ray
// CODE FROM THE OP
    $query = "SELECT * FROM products"; 
    $result = mysql_query($query); 
// MINIMUM NECESSARY TEST FOR SUCCESS
if (!$result) echo mysql_error();

Open in new window

And finally,
double check your directory permissions:

<?php
echo substr(sprintf('%o', fileperms(getcwd())), -4);
?>
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

works