• PHP Help PHP Coding
  • Would like to convert my MySQL driven script to include a flatfile option but...

I have not worked with flatfile data manipulation enough to know where to begin.

I understand that I would have to duplicate every part of the script that pulls or sends data to the database and modify it to work with a flatfile, but I was wondering if anyone knew of a start-to-finish basic tutorial for handling data when saving it this way. I have found small tuts at devshed and codewalkers, but have not found enough real world examples to put something together.

thanks,
json

    You'll probably need to read the file line by line, parse each line into its fields (you can use fgetcsv() to do all of that if it's a CSV file), and save the data into a 2-dimension array. (Dim. 1 will be the row number and dim. 2 will be the field number or name.)

    Then you can use all the various array functions that PHP has to search for data, sort it, etc. If you alter the data in the array, then you would have to rewrite the entire array contents over the text file, reversing the process used to read it into the array in the first place.

    All of this will convince you (if you aren't already convinced) that using a DBMS is much faster and more efficient. 🙂

      Hi there nogdog, and thanks very much for the reply.

      The only reason I'm contemplating it at all is because a few people now have written me that wanted to use the script and were jumping through hoops trying to get a remote db host working with the script on their shared account, and things of that sort. I thought a flatfile option would be handy for those with an extremely cheap hosting package.

      One question: Although you did an excellent job of persuading me not to do this(😉), my script is fairly simple, with basic retrievals and inputs. Would even the most basic script require the mind bending stuff you mentioned? I am being vague because I have no clue what you were trying to say in your post 😃

      thanks,
      json

        Perhaps you should look into using PHP's SQLLite extension? It provides SQL database functionality without using a separate DBMS server.

        The main problem with flat files is that you cannot randomly access lines for reading, updating, or inserting. The only thing you can do on an individual line basis is to append lines to the existing file. Thus, for any database sort of activity other than inserting new records, you will generally need to read the entire file, and overwrite it if making any changes (other than the aforementioned inserts appended to the end of the file).

        If you want to play around with it, here's a quick start on reading a CSV (comma-separated values) file into an array:

        $file = 'example.csv';
        $fh = fopen($file, 'r') or die("Cannot open CSV file for reading");
        $data = array();
        while(($line = fgetcsv($fh, 1000)) !== FALSE)
        {
           if(count($line) == 1 and $line[0] == NULL) // empty line
           {
              continue; // go to next line
           }
           $data[] = $line; // add this record to the data array
        }
        fclose($fh);
        // show retrieved data:
        printf("<pre>%s</pre>\n", print_r($data, 1));
        

          Thanks very much for the example and thoughts. Have you found the SQLLite extension to be one that is commonly built into PHP? It wouldn't do me any good to write it to use this extension if the guys that can't get a MySQL db also can't use the SQLLite module.

          If you don't think it's very common, do you know of any other alternative that might serve the purpose?

          thanks,
          json

            I've not used SQLLite yet, myself, as every situation I've run into so far has had MySQL available for use; so I can't really comment much about it other than that it exists and that I've heard a few people say they've used it quite successfully for some small-scale projects.

              Have you found the SQLLite extension to be one that is commonly built into PHP? It wouldn't do me any good to write it to use this extension if the guys that can't get a MySQL db also can't use the SQLLite module.

              According to the PHP manual: in PHP 5, the SQLite extension and the engine itself are bundled and compiled by default. However, since PHP 5.1.0 you need to manually activate the extension in php.ini (because it is now bundled as shared). Moreover, since PHP 5.1.0 SQLite depends on PDO it must be enabled too.

              I think that pretty much renders the SQLite extension useless for new scripts. The SQLite extension uses the SQLite 2.8.x format of database files (or file, rather, since each database is in exactly one file) while the SQLite driver from the PDO extension uses the SQLite 3.3.x (compatible with SQLite 3.4.0) format of database files.

              It also means that you do not have a guarantee that SQLite will be available for your script users since the PDO extension might not be enabled, especially if the MySQL extension is not enabled.

              If you don't think it's very common, do you know of any other alternative that might serve the purpose?

              It is very commonly available (at least where PHP5 is available, and now that PHP4 is reaching end of life, PHP5 should be available), but so is the MySQL extension (and even more so, since it was popular in PHP4). As such, if MySQL is not available, there would be reason to suspect that SQLite may not available. On the other hand, it is possible that since no database server needs to be setup and configured, a host may still offer SQLite support even without MySQL support.

                Write a Reply...