Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.28 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I import data from an XLS file to MySQL?
  2. <?php  
  3.     $db_username="root"; //database user name
  4.     $db_password="";//database password
  5.     $db_database="hr_mysql"; //database name
  6.     $db_host="localhost";
  7.  
  8.  
  9.     mysql_connect($db_host,$db_username,$db_password);
  10.     @mysql_select_db($db_database) or die( "Unable to connect to database.");
  11.  
  12.     $handle = fopen("UploadIt.xls", "r"); //test.xls excel file name
  13.     if ($handle)
  14.     {
  15.         $array = explode("n", fread($handle, filesize("UploadIt.xls")));
  16.     }
  17.  
  18.     $total_array = count($array);
  19.     $i = 0;
  20.     $Leave_Type_Id1="LTY001";
  21.     $Leave_Type_Id2="LTY002";
  22.  
  23.     while($i < $total_array)
  24.     {
  25.     $data = explode(",", $array[$i]);    
  26.     //$sql = "insert into test values ('$data[0]','$data[1]')";
  27.     $sql = "update `hs_hr_employee_leave_quota` set `no_of_days_allotted`= {$data[0]} WHERE `employee_id`= {$data[0]} and `leave_type_id`= '{$Leave_Type_Id1}'";        
  28.     $result = mysql_query($sql);
  29.     $sql = "update `hs_hr_employee_leave_quota` set `no_of_days_allotted`= {$data[2]} WHERE `employee_id`= {$data[0]} and `leave_type_id`= '{$Leave_Type_Id2}'";        
  30.     $result = mysql_query($sql);
  31.     $i++;
  32.     }
  33.     if($result==false)
  34.             echo "Not succed";  
  35.     else
  36.     {
  37.         echo "completed";  
  38.     }
  39.  
  40. ?>