Doing, Learning, and Sharing

Try to be The Best

Dec
17
2009

PHP read CSV File and insert to Mysql DB

Simple code for read CSV File and insert into mysql database per rows.

following this code:

PHP:
  1. mysql_connect("localhost","root","admin");
  2.      mysql_select_db("contact");
  3.      $filename="data.csv";
  4.      $handle = fopen("$filename", "r");
  5.      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
  6.      {
  7.        $import="INSERT into all_number(Name,Adress,Code) values('$data[0]','$data[1]','$data[2]')";
  8.        mysql_query($import) or die(mysql_error());
  9.       }
  10.      }
  11.      fclose($handle);
  12.    mysql_close();

I hope, This is helpfull for all

Nov
10
2009

Optimize indexing table on MySQL Database.

Optimize index is effect in celerity search query data in big database.
First time, you must specific category who you want to get, for example :

SELECT NAME, ADDRESS, POST_CODE FROM MEMBER WERE POST_CODE=’01221’

from this example, just only one category “POST_CODE” for search data
You must to create index to get maximum speed for result search data, follow this script:
ALTER TABLE `MEMBER` ADD INDEX `IDXPOSTCODE` (`POST_CODE`) ;

Explaining

ALTER TABLE ‘TABLE_NAME’ ADD INDEX ‘INDEX_NAME’ (‘NAME_FIELD’);
Name Field here is clause will be search data.
This example also can be use in more clause category for search, but every clause you have to give different name for INDEX_NAME.
Example :
SELECT NAME, ADDRESS, POST_CODE,CITY FROM MEMBER WERE POST_CODE=’01221’ AND CITY=’PONO’
Above example, any more clause category. You just get add more fields in INDEX_NAME, example :
ALTER TABLE ‘MEMBER’ ADD INDEX ‘IDX_POSTCITY’ (‘POST_CODE’,’CITY’);
Position of fields name in Syntax create Index must be following position in “SELECT” statement like as field POST_CODE is first and then CITY.
This is just sort article, maybe useful for all.

Powered by WordPress