PHP Classes

File: csv2mysql instructions

Recommend this page to a friend!
  Classes of Mark Williamson  >  csv2mysql database migration tool  >  csv2mysql instructions  >  Download  
File: csv2mysql instructions
Role: Documentation
Content type: text/plain
Description: instructions on how to use
Class: csv2mysql database migration tool
convert fields in a csv into SQL
Author: By
Last change:
Date: 19 years ago
Size: 5,678 bytes


Class file image Download


/* The order that the below are specified in is important. */

$i = new Import();

  debug() optional function - pass a 1 to it to turn debugging info on - 
  each function in the class will write a line explaining what its doing - 
 useful for finding the leaks etc or seeing at what stage something is failing
 or a lot of time is being taken. - defaults to 0 - off;

	- optional extra argument - true (debug(1,1)) will store up the messages
 into a variable - this can be retrieved with $i->get_error_messages() and 
	cleared out again with $->blank_error_messages()


/* connect to the database first. (do this first)  */

							//FILL IN THE BLANKS!!!! 
$i->db_connect("", "****", "********", "databasename");

/* read_csv_file - or read_big_csv_file - both the same now... function reads in the csv file */


  optional function call - if its a linux csv file you don't need to bother - but if its
  a windows generated csv file you do - because this function sets the linebreak char to look for
 - linux uses \n but windows uses \r\n - defaults to \n 


	Add the aliases (csv field -> mysql field) - so if you want pid in the csv file to be 
	entered as productID in the database ....  $i->add_field_alias("pid", "productID");

	do this step before reading in the csv file - otherwise the read function wont know how
	to set up the conversion arrays.

$i->add_field_alias("pid", "productID");
$i->add_field_alias("pname", "product_name");
$i->add_field_alias("bla", "description");

/* create_csv_data_array()
  this function needs to be called to create the different arrays needed by the class */


/* this function can duplicate a fields value in the final sql - it must be run after create_csv_data_array() */

$i->duplicate_field("bla", "desc2");

   adding sql and specifying the key (3rd arg) will cause the result from the query to be set 
	as element value.  you must make sure that only one field is returned and that it is returned 
	'as result' for this to work.  
		Note: this detects the "SELECT" word in arg2 - to know that its dealing with sql - 
		this should be ok but bare it in mind.

		 if you want an sql query to be run - call as follows:

			$i->add_db_instruction("itemname", 'SELECT itemname as 
												result FROM foo_item 
												WHERE uID="$this->dbkey"', "productID");
			the $this->dbkey is set by the thrid argument - in this case it is productID.
			so in this instance - the element itemID will be added to each element of the
			mysql_array which will ultimately be inserted into the database. the value for
			the new element itemID will be the result of the mysql query. 


$i->add_db_instruction("itemID", 'SELECT itemID as result FROM foo_item WHERE uID="$this->dbkey"', "productID");

	optionally you can run get_instruction_values() after adding an instruction -
	 then you can reference the key (a "dynamic key") and result of the last add_db_instuction as the key for the next 
	instruction  - OR if you dont need to reference dynamic keys then you can just run the
	get_instruction_values() once after adding all your instructions. 


/* add the next instruction - we can reference the dynamic key element itemID because we rand get_instruction_values() */

$i->add_db_instruction("SomeID", 'SELECT SomeID as result FROM foo_item WHERE itemID="$this->dbkey"', "itemID");

/* run it again - we've added more instructions */

/* not passing sql to this function (thus: no 3rd arg required) will cause a default value 
for the element to be set in each row. */

$i->add_db_instruction("price", "0.00");

$i->add_db_instruction("name", "name not set");

//once finished adding instruction values - run this function to do the magic...

	add_function_instruction - args: mysql fieldname, function name to call, arguments.
		to use this - fieldname, functionname, then: the arguments takes the names of
	existing elements in the mysql_array - so if you have productID and this is what the
	first argument of the function takes - then you would put productID in, followed by
	a comma - then the next argument ...and so on.
	- so in the below examples - 
	the call that is actually made from the below add_function_instruction is something like:

			$RefNum = get_refNum("1", "bmx");

$i->add_function_instruction("someField", "get_refNum2", "RefNum, product_name");
$i->add_function_instruction("barcode", "get_refNum", "productID, product_name");

	go through the function_instructions calling the functions and adding the values to 
	the final mysql_array variable 


/* create_sql()
	Creates the sql from the arrays that have been built up... will moan on errors 
	also - passing the optional argument (true) will make the output be echoed to the screen
  if you do not pass this arg then the sql will be returned from the function instead. 


	another optional debug function  displays some data arrays for seeing what data the class is holding 
	- pass 1 for mysql style  or  0 for dump_structures.


For more information send a message to info at phpclasses dot org.