3 Replies - 1780 Views - Last Post: 07 July 2012 - 04:33 AM Rate Topic: -----

#1 ankit2829  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Problem in inserting data from excel sheet to php myadmin by using php

Posted 06 July 2012 - 10:15 PM

Sir my project name is online quiz. i start working on my project. i design a database in excel-sheet. i want to import that data from excel-sheet to mysql database. sir how i proceed ??

kindly provide me some information

This post has been edited by Atli: 07 July 2012 - 12:21 AM
Reason for edit:: Bold font is meant to emphasize *parts* of your text, not the whole thing!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem in inserting data from excel sheet to php myadmin by using php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: Problem in inserting data from excel sheet to php myadmin by using php

Posted 07 July 2012 - 12:38 AM

View Postankit2829, on 07 July 2012 - 05:15 AM, said:

i design a database in excel-sheet.

That is your first error. Not that I have anything against Excel (that merits going into here), but it is not a good tool to use to design relational databases. Excel has little in common with servers like MySQL or even MSSQL. - In fact, just the fact that you designed it in Excel hints at serious design flaws if migrated over to a relational database.

You'd do better to use a tool built to work with the database you're using, which in case of MySQL could be MySQL Workbench.

View Postankit2829, on 07 July 2012 - 05:15 AM, said:

i want to import that data from excel-sheet to mysql database.

I'd say your best option for migrating data from Excel into a MySQL server is to export the data as a CSV file and use the LOAD DATA INFILE command to import it into a MySQL table. - Be sure to study the differences in the formats of the files. No doubt you'll have to use the optional parameters for the command to match the CSV format Excel uses. I highly doubt they'll match by default.
Was This Post Helpful? 1
  • +
  • -

#3 ankit2829  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-July 12

Re: Problem in inserting data from excel sheet to php myadmin by using php

Posted 07 July 2012 - 03:06 AM

Thank you sir for giving me your precious time.

acutly sir i am working in company. they are using excel sheet to put data on that. so i am bound to work under them

1: They provide all data in excelsheet database. now they want i import that data in my sql table using php code.

so i am not able to import all the values in mysql database.
how i proced .. kindly provide some suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: Problem in inserting data from excel sheet to php myadmin by using php

Posted 07 July 2012 - 04:33 AM

try this code

<?
    /********************************************************************************************/
    /* 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";
    ?>

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1