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
developerWorks - FREE Tools! |
Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
|
|
|
|
Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
|
|
|
|
XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |