Results 1 to 19 of 19

how to import data from excel to mysql using PHP

This is a discussion on how to import data from excel to mysql using PHP within the Programming forums, part of the Web Designing & Development category; how to import data from excel to mysql using PHP...

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    179

    Default how to import data from excel to mysql using PHP

    how to import data from excel to mysql using PHP

  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    4

    Default Re: how to import data from excel to mysql using PHP

    Hi,
    If there is only a few, you can enter it manually using something like phpMyAdmin or a tool such as Navicat. If there is too much to manually enter, you can use a software tool based on the import. Once again, Navicat here.Otherwise can help, you can write the script itself using PHP to import your Excel file to scan and enter data into the database. Be sure to save your Excel file as a CSV file.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4

    Default Re: how to import data from excel to mysql using PHP

    Explore fgetcsv()

    php.net/manual/en/function.fgetcsv.php

  4. #4
    Member
    Join Date
    Nov 2010
    Posts
    99

    Default Re: how to import data from excel to mysql using PHP

    Quote Originally Posted by ExpertsGuide View Post
    Explore fgetcsv()

    php.net/manual/en/function.fgetcsv.php
    That's realy cool

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    25

    Default Re: how to import data from excel to mysql using PHP

    The simplest way to use Excel imports and exports with PHP is to use the PEAR Excel functions. I've just finished working on a project where I've had to export to Excel directly from PHP, and another developer produced the import script, and it's all pretty easy wen you use this.

  6. #6
    Senior Member maxelink's Avatar
    Join Date
    Nov 2010
    Location
    Webmaster
    Posts
    143

    Default Re: how to import data from excel to mysql using PHP

    hi mate ,

    Excel-MySQL is a very easy to use , yet powerful , tool to import any Excel spreadsheets into MySQL database or publish any MySQL database as an Excel file . Supporting many advanced features like Conversion schedule, Selecting certain tables or fields , command line , .etc

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    25

    Default Re: how to import data from excel to mysql using PHP

    Try this... It may help you to solve your problem.

    Code:
    PHP Syntax
     
    <?
     
    /********************************************************************************************/
    /* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
    /* Edit the entries below to reflect the appropriate values
    /********************************************************************************************/
    $databasehost = "localhost";
    $databasename = "test";
    $databasetable = "sample";
    $databaseusername ="test";
    $databasepassword = "";
    $fieldseparator = ",";
    $lineseparator = "\n";
    $csvfile = "E:\Webcache\AA ONLINE\test1\english1.csv";
    /********************************************************************************************/
    /* Would you like to add an ampty field at the beginning of these records?
    /* This is useful if you have a table with the first field being an auto_increment integer
    /* and the csv file does not have such as empty field before the records.
    /* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
    /* This can dump data in the wrong fields if this extra field does not exist in the table
    /********************************************************************************************/
    $addauto = 0;
    /********************************************************************************************/
    /* Would you like to save the mysql queries in a file? If yes set $save to 1.
    /* Permission on the file should be set to 777. Either upload a sample file through ftp and
    /* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
    /********************************************************************************************/
    $save = 1;
    $outputfile = "output.sql";
    /********************************************************************************************/
     
     
    if(!file_exists($csvfile)) {
    	echo "File not found. Make sure you specified the correct path.\n";
    	exit;
    }
     
    $file = fopen($csvfile,"r");
     
    if(!$file) {
    	echo "Error opening data file.\n";
    	exit;
    }
     
    $size = filesize($csvfile);
     
    if(!$size) {
    	echo "File is empty.\n";
    	exit;
    }
     
    $csvcontent = fread($file,$size);
     
    fclose($file);
     
    $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
    @mysql_select_db($databasename) or die(mysql_error());
     
    $lines = 0;
    $queries = "";
    $linearray = array();
     
    foreach(split($lineseparator,$csvcontent) as $line) {
     
    	$lines++;
     
    	$line = trim($line," \t");
     
    	$line = str_replace("\r","",$line);
     
    	/************************************************************************************************************
    	This line escapes the special character. remove it if entries are already escaped in the csv file
    	************************************************************************************************************/
    	$line = str_replace("'","\'",$line);
    	/***********************************************************************************************************/
     
    	$linearray = explode($fieldseparator,$line);
     
    	$linemysql = implode("','",$linearray);
     
    	if($addauto)
    		$query = "insert into $databasetable values('','$linemysql');";
    	else
    		$query = "insert into $databasetable values('$linemysql');";
     
    	$queries .= $query . "\n";
     
    	@mysql_query($query);
    }
     
    @mysql_close($con);
     
    if($save) {
     
    	if(!is_writable($outputfile)) {
    		echo "File is not writable, check permissions.\n";
    	}
     
    	else {
    		$file2 = fopen($outputfile,"w");
     
    		if(!$file2) {
    			echo "Error writing to the output file.\n";
    		}
    		else {
    			fwrite($file2,$queries);
    			fclose($file2);
    		}
    	}
     
    }
     
    echo "Found a total of $lines records in this csv file.\n";
     
     
    ?>

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Location
    Karachi, Pakistan
    Posts
    1

    Default Re: how to import data from excel to mysql using PHP

    First convert xls file to .csv then use fgetcsv() function.

  9. #9
    Member
    Join Date
    Nov 2010
    Posts
    80

    Default Re: how to import data from excel to mysql using PHP

    Thank you for sharing the your Experience..

  10. #10
    Junior Member
    Join Date
    Aug 2010
    Posts
    21

    Default Re: how to import data from excel to mysql using PHP

    I encounter these kinds of questions before, thanks for sharing.


  11. #11
    Senior Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    116

    Default Re: how to import data from excel to mysql using PHP

    Hello Friends,

    The simplest way to use Excel imports and exports with PHP is to use the PEAR Excel functions. I've just finished working on a project where I've had to export to Excel directly from PHP, and another developer produced the import script, and it's all pretty easy wen you use this.

    Thanks

  12. #12
    Junior Member
    Join Date
    Mar 2011
    Posts
    12

    Default Re: how to import data from excel to mysql using PHP

    The easiest way to use Excel in PHP import and export of Excel function using PEAR. I just finished working there I have to export to Excel directly from the PHP project, and the other development and production of the import script, it's all very easy to use this news.

  13. #13
    Member
    Join Date
    Mar 2011
    Posts
    62

    Default Re: how to import data from excel to mysql using PHP

    Hi,
    thanks a lot for sharing your knowledge with all.it is a very helpful tool.now I have got the overall idea about the procedure of import data from excel to mysql using PHP.I want to know about the procedure.I definitely use this tool.


  14. #14
    Junior Member
    Join Date
    Dec 2011
    Posts
    16

    Lightbulb Re: how to import data from excel to mysql using PHP

    the best way is to create a *csv file of your excel file and import it via php then yuo can use it as sql database.




    http://www.webyukti.com

  15. #15
    Senior Member
    Join Date
    Jul 2011
    Posts
    189

    Default Re: how to import data from excel to mysql using PHP

    Hey,

    Do check out this link http://blog.tjitjing.com/index.php/2...in-5-easy.html . I think it is really useful and shows easy to follow steps regarding the matter. Nonetheless, PEAR excel functions are pretty effective as well.
    Last edited by Jeniekrag; 12-13-2011 at 01:25 PM.

  16. #16
    Junior Member
    Join Date
    Feb 2012
    Posts
    1

    Default Re: how to import data from excel to mysql using PHP

    Hello Friends
    Try this code:
    <?php

    require_once 'Excel/reader.php';
    $data = new Spreadsheet_Excel_Reader();

    $data->read('Senator.xls');

    error_reporting(E_ALL ^ E_NOTICE);
    echo "<table border='1'>";
    echo "<tr><th>STT</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Email ID</th></tr>";

    for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
    {
    echo "<tr>";

    echo "<td>";
    echo $data->sheets[0]['cells'][$j+1][1];
    echo "</td>";
    echo "<td>";
    echo $data->sheets[0]['cells'][$j+1][2];
    echo "</td>";
    echo "<td>";
    echo $data->sheets[0]['cells'][$j+1][3];
    echo "</td>";

    echo "<td>";

    echo $data->sheets[0]['cells'][$j+1][4];
    echo "</td>";

    echo "<td>";

    echo $data->sheets[0]['cells'][$j+1][5];
    echo "</td>";
    //echo "<br>";

    echo "</tr>";

    }

    echo "</table>";

    ?>

  17. #17
    Member
    Join Date
    Jan 2012
    Posts
    61

    Default Re: how to import data from excel to mysql using PHP

    Quote Originally Posted by ExpertsGuide View Post
    Explore fgetcsv()

    php.net/manual/en/function.fgetcsv.php
    Thank you so much!!
    For more resource please check or Visit Photo Clipping, Clipping Images

  18. #18
    Junior Member
    Join Date
    Feb 2012
    Posts
    15

    Default Re: how to import data from excel to mysql using PHP

    really that code was helpful

  19. #19
    Junior Member
    Join Date
    Feb 2012
    Posts
    15

    Default Re: how to import data from excel to mysql using PHP

    import using pear