Database Code
  Home arrow Database Code arrow Load a file into a database or somethi...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Best practices for software analysis: An introduction to the IBM Rational Software Analyzer application

    This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer.
    FREE! Go There Now!


    NEW! Rational Build Forge Express eKit

    Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Method Composer V7.2

    Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Performance Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    DATABASE CODE ARTICLES

    - 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
    - CSV to SQL convertor





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek