Database Code

  Home arrow Database Code arrow Load a file into a database or somethi...
DATABASE CODE

Load a file into a database or something
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This class will load a file that is separated by anything into a table in a database. There is an example on how this is done at the start of the file.. so.. ehmm.. enjoy

    By : Scha

    <?php

    /* loadtodb.php

    Class to load n/a separated files into a database
    (c) Andreas Bernhardsen
    scha_8@yahoo.com
    */

    /* How to use it
    *
    * The first thing you need to do is to load the class with the constructor
    *
    * $load = new LoadToDB('members', 'myFile.txt', 1 [, ',']);
    *
    * Where 'members' is the name of the table to input into and 'myFile.txt'
    * is the file containing the data to be inputed. third argument (0)
    * spesifies if the first line of the file is a heading file.
    * if you spesifie this the first line of the file will not be inserted
    *
    * The last argument is optional, if you dont pass anything it will assume
    * that the file is comma (,) separated. you can pass other things like
    * "\t" (tab)
    *
    * after you have constructed the class you need to specify the fields you
    * want inputed. This you need to pass as an array
    *
    * example: here is a file
    *
    * NAME, ADDRESS, PHONE, FAX, CELLPHONE, MEMBER NR
    * Andreas Bernhardsen, none, 11111111, 11111112, 90909090, 1
    * John Doe, Rd drive 1, 22222222, 22222223, 91919191, 2
    * Jane Doe, Rd drive 2, 33333333, 33333334, 92929292, 3
    *
    * Lets say my members table contain 3 fields:
    * name, phone, member_nr
    * I dont really need address, fax and cellphone information
    * so you say:
    *
    * $load->SetFields(array(0 => 'name', 2 => 'phone', 5 => 'member_nr'));
    *
    * Can you see where I'm heading at? The number you put it into secifies how
    * commas (,) befor the data.
    *
    * after you have done that you only need to call:
    *
    * $sucess = $load->LoadIntoDB();
    *
    * this will return 1 on success and 0 on failure
    * be sure you have connected to the mysql database befor executing this
    * last function.
    *
    * send me e-mail at scha_8@yahoo.com with bugs or sugestions
    *
    */

    class LoadToDB
    {
    /* Declare class variables */
    var $dbname;
    var $dbfields;
    var $dbfile;
    var $field_num;
    var $a_keys;
    var $separator;
    var $head;

    /* The constructor */
    function LoadToDB($name, $file, $head, $separator = ',')
    {
    if($head)
    $this->head = 1; /* if head is set */
    $this->separator = $separator; /* define separator */
    $this->dbname = $name; /* wich table do we wanna insert into */
    $this->dbfile = file("$file"); /* load the file into an array */
    }

    function SetFields($fields)
    {
    $this->dbfields = $fields; /* copy */
    $this->field_num = count($this->dbfields); /* count */
    $this->a_keys = array_keys($this->dbfields); /* find keys */
    }

    function LoadIntoDB()
    {
    $rows = count($this->dbfile);
    /* make the sting that is identical to all querys */
    $start_query = "INSERT INTO ".$this->dbname." (";
    for($i = 0; $i < $this->field_num; $i++)
    {
    if($i != 0)
    $start_query .= ", ";
    $start_query .= $this->dbfields[$this->a_keys[$i]];
    }
    $start_query .= ") VALUES (";
    /* Loop through all entrys and insert them */
    for($i = $this->head; $i < $rows;$i++)
    {
    $query = $start_query;
    $this->dbfile[$i] = addslashes($this->dbfile[$i]);
    $n_row = explode($this->separator, $this->dbfile[$i]);
    for($j = 0; $j < $this->field_num; $j++)
    {
    if($j != 0)
    $query .= ", ";
    $query .= "'".$n_row[$this->a_keys[$j]]."'";
    }
    $query .= ")";
    mysql_query($query);
    if(mysql_affected_rows() < 1)
    return 0; // on failure
    }
    return 1; // on success
    }
    }

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap