0 Replies - 2651 Views - Last Post: 07 February 2011 - 11:08 PM Rate Topic: -----

#1 vishal chander  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 11

import data from excel sheet into mysql database for php

Posted 07 February 2011 - 11:08 PM


  $data = array();   
   function add_person( $first, $middle, $last, $email ) 
    {
	  global $data;   
	   $data []= array(  'first' => $first,  'middle' => $middle,  'last' => $last,  'email' => $email   ); 
	    } 
		   if ( $_FILES['file']['tmp_name'] )
		     { 
			  $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
			  
			    $rows = $dom->getElementsByTagName( 'Row' );
				  $first_row = true; 
				   foreach ($rows as $row) 
				    {  if ( !$first_row )
					  {
					    $first = ""; 
						 $middle = ""; 
						  $last = ""; 
						   $email = "";  
						     $index = 1; 
							  $cells = $row->getElementsByTagName( 'Cell' );  
							  foreach( $cells as $cell ) 
							   { 
							     $ind = $cell->getAttribute( 'Index' ); 
								  if ( $ind != null ) $index = $ind; 
								     if ( $index == 1 ) $first = $cell->nodeValue; 
									  if ( $index == 2 ) $middle = $cell->nodeValue; 
									   if ( $index == 3 ) $last = $cell->nodeValue; 
									    if ( $index == 4 ) $email = $cell->nodeValue;  
										  $index += 1; 
										   }  
										   add_person( $first, $middle, $last, $email );
										     } 
											  $first_row = false; 
											   } 
											    }
							  $username = "root";  $password = "";  $hostname = "localhost";   
							 $con = mysql_connect($hostname,$username,$password) ;
							 //or die("Unable to connect to MySQL"); 
						 mysql_select_db("example_import",$con); 
						  mysql_error();  
						  foreach($data as $row)  
						 
						 {  
			  mysql_query("insert into import_data values('','$row[first]','$row[middle]','$row[last]','$row[email]')");
			    } 
				 ?>





the above is my code which is actually working. But i want it can Directly Import Data from Excel sheet into mysql database for php, without any conversion in another format like xml spreadsheet and .csv file.
So, Please Help me how can i do this.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1