Jump to content

[SOLVED] reading excel sheet and storing in mysql


shane07

Recommended Posts

Simply export data from excel as TXT or CSV.

 

then

 

LOAD DATA INFILE 'path/file.csv' INTO TABLE your_table; 

 

Is that the sql statement that can be used in php script?

Thank you for ur reply but what should I do if I have more than two tables referenced to same excel sheet

Link to comment
Share on other sites

use php pear classes for that

 

or else if you are using this feature on windows you can create the datasource with excel and query the excel sheet to retrieve the values.

 

Thank you I m using windows for testing. But using linux for online testing. Does the php pear class is supported by both windows and linux?

Link to comment
Share on other sites

I haven't tested this but it should work for a sheet saved in txt format

 

<pre>
<?php
$con=mysql_connect();
mysql_select_db('test');
$sheet='test.txt';
$file=fopen($sheet,'r');
while(!feof($sheet)){
$line=fgets($sheet);
$line=explode("\t",$line);
$query='INSERT INTO `table` VALUES (';
foreach($line as $key => &$value){
	if($$key==0){
		$query.="'".$value."'";
	}else{
		$query.=", '".$value."'";
	}
}
$query.=');';
echo $query."\n";
mysql_query($query,$con);

}
fclose($file);


?>
</pre>

Or if you save it as CSV change the explode from "\t" to ","

 

Hope this helps.

Scott.

Link to comment
Share on other sites

I haven't tested this but it should work for a sheet saved in txt format

 

<pre>
<?php
$con=mysql_connect();
mysql_select_db('test');
$sheet='test.txt';
$file=fopen($sheet,'r');
while(!feof($sheet)){
$line=fgets($sheet);
$line=explode("\t",$line);
$query='INSERT INTO `table` VALUES (';
foreach($line as $key => &$value){
	if($$key==0){
		$query.="'".$value."'";
	}else{
		$query.=", '".$value."'";
	}
}
$query.=');';
echo $query."\n";
mysql_query($query,$con);

}
fclose($file);


?>
</pre>

Or if you save it as CSV change the explode from "\t" to ","

 

Hope this helps.

Scott.

 

this means I have to remove the titles(field titles) from the excel sheet. Otherwise the field titles are also inserted into the database, right?

 

But the code have two errors:

while(!feof($sheet)){ //should be $file instead of $sheet, right?
$line=fgets($sheet);

 

Thank you for helping me

Link to comment
Share on other sites

I haven't tested this but it should work for a sheet saved in txt format

 

<pre>
<?php
$con=mysql_connect();
mysql_select_db('test');
$sheet='test.txt';
$file=fopen($sheet,'r');
while(!feof($sheet)){
$line=fgets($sheet);
$line=explode("\t",$line);
$query='INSERT INTO `table` VALUES (';
foreach($line as $key => &$value){
	if($$key==0){
		$query.="'".$value."'";
	}else{
		$query.=", '".$value."'";
	}
}
$query.=');';
echo $query."\n";
mysql_query($query,$con);

}
fclose($file);


?>
</pre>

Or if you save it as CSV change the explode from "\t" to ","

 

Hope this helps.

Scott.

 

this means I have to remove the titles(field titles) from the excel sheet. Otherwise the field titles are also inserted into the database, right?

 

But the code have two errors:

while(!feof($sheet)){ //should be $file instead of $sheet, right?
$line=fgets($sheet);

 

Thank you for helping me

The topic is solved but is there a way to exclude the field titles of the excel sheet while reading? Waiting for ur reply.

Thank You

Link to comment
Share on other sites

If all your titles are on the first or second lines you could exclude those lines from the the function

change your code to this.

 

<pre>
<?php
$con=mysql_connect();
mysql_select_db('test');
//number of lines to skip
$skip=1;
$current_line=1;


$sheet='test.txt';
$file=fopen($sheet,'r');
while(!feof($file)){
$line=fgets($file);
//add this
if($current_line>$skip){


	$line=explode("\t",$line);
	$query='INSERT INTO `table` VALUES (';
	foreach($line as $key => &$value){
		if($$key==0){
			$query.="'".$value."'";
		}else{
			$query.=", '".$value."'";
		}
	}
	$query.=');';
	echo $query."\n";
	mysql_query($query,$con);
//add this
}
$current_line++;


}
fclose($file);


?>
</pre>

 

Sorry my server is still down so i cannot test it.

but i have fixed the $sheet to $file sorry about that.

 

Scott.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.