[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

php writing to a file

Posted on 2010-01-04
9
Medium Priority
?
311 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 12

Expert Comment

by:jet-black
ID: 26175224
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);
?>
0
 
LVL 13

Expert Comment

by:marchent
ID: 26175246
use fwrite instead of fputcsv
fwrite($fp, "hello\n");
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26175273
@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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:hielo
ID: 26175282
>>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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26175345
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

0
 
LVL 12

Expert Comment

by:jet-black
ID: 26175375
And finally,
double check your directory permissions:

<?php
echo substr(sprintf('%o', fileperms(getcwd())), -4);
?>
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26176292
BTW: on my post change:
while ($row = mysql_fetch_array($result)){

to:
while ($row = mysql_fetch_array($result, MYSQL_NUM)){


...
while ($row = mysql_fetch_array($result, MYSQL_NUM)){ 
        fputcsv($fp, $row); 
    } 
...

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 31672699
works
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question